View Full Version : simple math, Java

Sep 18, 2006, 12:51 PM
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!

Sep 18, 2006, 12:53 PM
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. :)

Sep 18, 2006, 12:54 PM
operator / cannot be applied to java.lang.String,java.lang.String

ANyone have any ideas? Thanks!You have to cast the String to another data type.

Tryin Integer.parseInt(String) or Double.parseDouble(String)

Sep 18, 2006, 12:59 PM
import javax.swing.*;

public class Learning
public Learning()
String noa = "";
String nob = "";
double noc = noa*nob;

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 ();




Sep 18, 2006, 01:03 PM
Like Chris has already said, you cannot apply math functions to string data types. So, for example...


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

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

Sep 18, 2006, 01:04 PM
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.


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) );

Sep 18, 2006, 03:02 PM
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.



Sep 18, 2006, 11:33 PM
@ 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!

Sep 19, 2006, 11:06 AM
here it is, it multiplies