What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Newbie Java Question

Newbie Java Question
Author: Message:
albert
Veteran Member
*****

Avatar

Posts: 2247
Reputation: 42
– / Male / Flag
Joined: Feb 2005
O.P. Roll Eyes  Newbie Java Question
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
       
    }

}


This post was edited on 02-09-2010 at 09:57 PM by albert.
09-10-2006 03:57 AM
Profile E-Mail PM Web Find Quote Report
ShawnZ
Veteran Member
*****

Avatar

Posts: 3146
Reputation: 43
32 / Male / Flag
Joined: Jan 2003
RE: Newbie Java Question
strings are text, not numbers. you're adding two lines of text together, lol. use ints for += and -= to function as math operators.
Spoiler:
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
09-10-2006 04:03 AM
Profile PM Web Find Quote Report
albert
Veteran Member
*****

Avatar

Posts: 2247
Reputation: 42
– / Male / Flag
Joined: Feb 2005
O.P. RE: Newbie Java Question
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

09-10-2006 04:04 AM
Profile E-Mail PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: Newbie Java Question
result = number1 += number2 ;

should be:

result = number1 + number2 ;

tip: if you don't know what something does, don't use it.
The previous sentence is false. The following sentence is true.
09-10-2006 12:52 PM
Profile PM Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: Newbie Java Question
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.
[Image: spartaafk.png]
09-11-2006 11:34 PM
Profile PM Web Find Quote Report
albert
Veteran Member
*****

Avatar

Posts: 2247
Reputation: 42
– / Male / Flag
Joined: Feb 2005
O.P. RE: Newbie Java Question
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.. :$
09-12-2006 01:13 AM
Profile E-Mail PM Web Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: Newbie Java Question
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.
[Image: spartaafk.png]
09-12-2006 03:02 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On