Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 18, 2006, 12:51 PM   #1
Jasonbot
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
simple math, Java

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!
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 18, 2006, 12:53 PM   #2
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: 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.
__________________
My 24 hour web cam!
SilentPanda is offline   0 Reply With Quote
Old Sep 18, 2006, 12:54 PM   #3
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Quote:
Originally Posted by Jasonbot
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)
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Sep 18, 2006, 12:59 PM   #4
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
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
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 18, 2006, 01:03 PM   #5
darkcurse
macrumors 6502a
 
darkcurse's Avatar
 
Join Date: Nov 2005
Location: 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.
darkcurse is offline   0 Reply With Quote
Old Sep 18, 2006, 01:04 PM   #6
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: 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) );
__________________
My 24 hour web cam!
SilentPanda is offline   0 Reply With Quote
Old Sep 18, 2006, 03:02 PM   #7
larswik
macrumors 65816
 
Join Date: Sep 2006
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
larswik is offline   0 Reply With Quote
Old Sep 18, 2006, 11:33 PM   #8
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
@ larswik, I'm just trying to get ahead of the class so you're way in front now!

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

Edit: THANKS EVERYONE! You guys rock!
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 19, 2006, 11:06 AM   #9
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
here it is, it multiplies
http://rapidshare.de/files/33681093/Learning.jar.html
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
3rd Grade Math: Summer Review Splash Math App klkatielong iPad Apps 0 Jul 18, 2012 12:56 PM
[Math App] Homework teaches kids math and reports to parents Gaea deBomb iPhone and iPod touch Apps 0 Jul 3, 2012 04:35 AM
Pictures on Simple Java GUI won't work. macuser1232 Mac Programming 1 Jun 26, 2012 11:17 PM
A Middle and High School Math App: Cool Math mathfantasia iPad Apps 2 Jun 19, 2012 06:35 PM
5+7+10+5+2+3= ? Math Aloud ! true mental math ... left & right brain. mobile.esd.mx iPhone and iPod touch Apps 0 Jun 1, 2012 04:17 PM

Forum Jump

All times are GMT -5. The time now is 09:50 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC