simple math, Java

Discussion in 'Mac Programming' started by Jasonbot, Sep 18, 2006.

  1. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Messages:
    2,468
    Location:
    The Rainbow Nation RSA
    #1
    Hi all, I'm trying to make a really simple maths program using java and JOptionPane's!? The thing is I can't get th enumbers to work with eachother, it keeps saying this:
    operator / cannot be applied to java.lang.String,java.lang.String

    ANyone have any ideas? Thanks!
     
  2. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Messages:
    9,787
    Location:
    The Bamboo Forest
    #2
    Post some code. That could mean a lot of things depending on the code. But it looks like you might be trying to do math (division) with Strings....

    Use the
    Code:
     tags too.  :)
     
  3. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Messages:
    2,294
    Location:
    Huntsville, AL
    #3
    You have to cast the String to another data type.

    Tryin Integer.parseInt(String) or Double.parseDouble(String)
     
  4. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Messages:
    2,468
    Location:
    The Rainbow Nation RSA
    #4
    Code:
    import javax.swing.*;
    
    public class Learning 
    {
    		public Learning()
    		{
    			//values
    			String noa = "";
    			String nob = "";
    			double noc = noa*nob;
    			
    				
    			//other
    			String header = "Calculator";
    			float number = 1345213f;
    			int hi1 = 31254;
    			int hi2 = 345;
    			double hi3 = hi1/hi2;
    			char yes= 'y';
    			
    			
    			noa = JOptionPane.showInputDialog(null, "value 1?", "What do u wanna know?", JOptionPane.QUESTION_MESSAGE);
    			JOptionPane.showConfirmDialog(null,noa , "number1", JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE);
    			nob = JOptionPane.showInputDialog(null, "value 2?", "What do u wanna know?", JOptionPane.QUESTION_MESSAGE);
    			JOptionPane.showConfirmDialog(null,nob , "number2", JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE);
    			JOptionPane.showConfirmDialog(null, "Number 1 = " +noa +"\n Number 2 = " +nob +"\n Answer = " +noc, "test", JOptionPane.PLAIN_MESSAGE, JOptionPane.ERROR_MESSAGE);
    			
    		}
    			public static void main(String args[]) 
    	
    {
    	new Learning ();
    			
    }
    
    }
    
    THANKS
     
  5. macrumors 6502a

    darkcurse

    Joined:
    Nov 5, 2005
    Messages:
    536
    Location:
    Sydney
    #5
    Like Chris has already said, you cannot apply math functions to string data types. So, for example...

    After:

    noa = JOptionPane.....
    noaa = Integer.ParseInt(noa);
    ...
    ...

    And so forth and so on so that your program can do the math.
     
  6. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Messages:
    9,787
    Location:
    The Bamboo Forest
    #6
    Can't multiply strings...

    String noc = noa*noc;

    Use Integer.parseInt as stated by another poster. That is if you're expecting ints... otherwise use the Double.parseDouble above.

    BUT

    You are calculating the value of noa * nob while they are both set to "". So you'll still get an error. Calculate the value after the user has input the numbers.

    You could also verify that they are numbers before doing the computation but since you're learning it's probably not a necessity at this point.

    Also I think you want noc to be noa * nob. Right now noc is set to be noa * noc.

    You'll also want to convert the multiplied ints back to a String...

    String noc = Integer.toString( Integer.parseInt(noa) * Integer.parseInt(nob) );
     
  7. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #7
    I have to laugh a little Jason. You are talking a class to teach you Java and I bought a Java for Dummies book that I am learning from. Last night Istarted learnign the next part of the book and it is also regarding the PARSE section. the next section I go into is Type Casting and Shadowing Variables.

    It's like your class is being tought from the book I am reading, or perhaps this is just the way it is done.

    Cheers!

    -Lars
     
  8. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Messages:
    2,468
    Location:
    The Rainbow Nation RSA
    #8
    @ larswik, I'm just trying to get ahead of the class:D so you're way in front now!

    I got it to work, my code's a bit weired but it works :D:D:D!!!

    Edit: THANKS EVERYONE! You guys rock!
     
  9. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Messages:
    2,468
    Location:
    The Rainbow Nation RSA

Share This Page