Newbie Java Question - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Newbie Java Question (/showthread.php?tid=66026)
Newbie Java Question by albert on 09-10-2006 at 03:57 AM
So I decided I'd give programming a shot.. I got stuck there.. lol..
The answer to my addition ( number 1 += number 2 ) gives me 22 when I enter 2 and 2.. anyways.. can anyone see what's wrong with this code? lol thanks
code:
//***********************************
//Program Information
//***********************************
//Date Created : 09-09-06
//***********************************
//Goal : Simple Addition
//***********************************
import javax.swing.JOptionPane;
public class Alby2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String number1 ; // First Number To Add String
String number2 ; // Second Number To Add String
String result ; // Result String
String show ; // To Show Text String
number1 = JOptionPane.showInputDialog ( "Enter Your First Number To Add") ; // Reading Number 1
number2 = JOptionPane.showInputDialog ( "Enter Your Second Number To Add" ) ; // Reading Number 2
result = number1 += number2 ; // Adding Up Both Numbers
show = "The Result Is " + result ; // Text + Answer
JOptionPane.showMessageDialog (null , show ) ; // Show Text
System.exit ( 0 ) ; // End Program
}
}
RE: Newbie Java Question by ShawnZ on 09-10-2006 at 04:03 AM
strings are text, not numbers. you're adding two lines of text together, lol. use ints for += and -= to function as math operators.
RE: Newbie Java Question by albert on 09-10-2006 at 04:04 AM
quote: Originally posted by ShawnZ
strings are text, not numbers. you're adding two lines of text together, lol. use ints for += and -= to function as math operators.
hum.. woops..
* albert runs in shame
RE: Newbie Java Question by segosa on 09-10-2006 at 12:52 PM
result = number1 += number2 ;
should be:
result = number1 + number2 ;
tip: if you don't know what something does, don't use it.
RE: Newbie Java Question by RaceProUK on 09-11-2006 at 11:34 PM
quote: Originally posted by alby
Auto-generated method stub
You're just starting Java yes? Stop using an IDE! You won't learn the language effectively while you use one.
RE: Newbie Java Question by albert on 09-12-2006 at 01:13 AM
quote: Originally posted by RaceProUK
quote: Originally posted by alby
Auto-generated method stub
You're just starting Java yes? Stop using an IDE! You won't learn the language effectively while you use one.
That's what my teacher at school told me to use..
RE: Newbie Java Question by RaceProUK on 09-12-2006 at 03:02 PM
Then your teacher doesn't know how to teach programming. When I first learnt Java, I was encouraged to use a text editor and the command line. Same when I started learning C and C++ properly. As a result I understand how the languages work, and can therefore use them better.
I learnt VB in an IDE, and am comparatively crap at it, despite knowing it longer.
|