Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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!
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
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.  :)
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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
 

darkcurse

macrumors 6502a
Nov 5, 2005
538
0
Sydney
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.
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
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) );
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
@ 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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.