Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 input
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Tue May 02, 2006 8:53 pm   Post subject: (No subject)

yes Scanner is capitalized! here is my code

code:

import java.util.*;
public class calculator2
{
        public static void main (String[] args)
        {
            Scanner in = new Scanner (System.in);
            int num1;
            int num2;
            int sum;
            
            System.out.println("Enter first number");
            num1 = in.nextInt();
            System.out.println("Enter Second number");
            num2 = in.nextInt();
            sum = num1 + num2;
            System.out.println("The sum of the number is: " + sum);
           }
            
}
'

maybe i don't have the right version Confused if this works on your computers can u please tell me exactly which website u downloaded java platform 1.5?
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Tue May 02, 2006 9:15 pm   Post subject: (No subject)

Yes, it works.

I believe this is one of the reasons why wtd doesn't recommend IDEs, especially for the beginner. It's much simpler and more straight forward to just download the SDK from java.sun.org and user a text editor and the command line to do your programming.
cool dude




PostPosted: Tue May 02, 2006 9:22 pm   Post subject: (No subject)

unforunately i can see i'm not going to be using your way. Sad so back to using the old way which i will remember soon enough! Smile so back to my question how can i convert the string variable to an integer. i saw the link tony gave me but i can't really understand it Confused
cool dude




PostPosted: Tue May 02, 2006 9:31 pm   Post subject: (No subject)

nm finally figured how how to convert them!

code:

Inum1 = Integer.parseInt(Snum1);


right?
and wat does parseInt mean?

p.s. how do i error proof it so that if the user enters a letter it won't crash?
wtd




PostPosted: Tue May 02, 2006 9:34 pm   Post subject: (No subject)

cool dude wrote:
nm finally figured how how to convert them!

code:

Inum1 = Integer.parseInt(Snum1);


right?
and wat does parseInt mean?

p.s. how do i error proof it so that if the user enters a letter it won't crash?


First off: variable names should not ever ever ever begin with a capital letter.

Second: what does "parse" mean? Look it up in a dictionary. Smile

Lastly: What happens if a user does enter something other than a valid number?
cool dude




PostPosted: Tue May 02, 2006 10:05 pm   Post subject: (No subject)

wtd wrote:
cool dude wrote:
nm finally figured how how to convert them!

code:

Inum1 = Integer.parseInt(Snum1);


right?
and wat does parseInt mean?

p.s. how do i error proof it so that if the user enters a letter it won't crash?


First off: variable names should not ever ever ever begin with a capital letter.

Second: what does "parse" mean? Look it up in a dictionary. Smile

Lastly: What happens if a user does enter something other than a valid number?


1) may i ask why variable names should never start with a capital letter? this was just a sample program i made i'm usually too lazy to always type in capitals but i still wanna know the reasoning?

2) is that a rhetorical question? because i asked u how can i error proof that if the user enters something thats not a valid number?
Tony




PostPosted: Tue May 02, 2006 10:11 pm   Post subject: (No subject)

1 - capitalized names are for class names: String, Integer, Scanner, etc

2 - it's not a rhetorical question. Find out what happens when the user enters something that is not a valid number. (hint: try it out with your code)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
cool dude




PostPosted: Tue May 02, 2006 10:20 pm   Post subject: (No subject)

k i don't think u guys understood me. i know wat happens thats why i posted my question!!! how can i error proof that? how can i fix it from crashing when u enter a letter or anything but a number.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue May 02, 2006 10:28 pm   Post subject: (No subject)

Java programs don't generally just crash. What is actually happening when invalid input occurs?
cool dude




PostPosted: Tue May 02, 2006 10:29 pm   Post subject: (No subject)

finally figured out how to error proof that Smile

code:

try{
        Inum1 = Integer.parseInt(Snum1);
        Inum2 = Integer.parseInt(Snum2);
        sum = Inum1 + Inum2;
        System.out.println("The sum of the numbers is " + sum);
        }catch (NumberFormatException nfe){
        System.out.println("can't convert: " + Snum1);
   
    }


now the only thing i don't understand is after NumberFormatException there is "nfe" what does that mean? and is it something i just need to memorize?
[Gandalf]




PostPosted: Tue May 02, 2006 10:43 pm   Post subject: (No subject)

nfe can be renamed to almost anything else (any value variable name), because that's what it is. It contains the NumberFormatException that is being caught. For example you could have:
code:
catch (NumberFormatException e) {
     System.err.println(e);
}

Try it out!
wtd




PostPosted: Tue May 02, 2006 10:46 pm   Post subject: (No subject)

Perhaps you should read up on exception handling a bit more. Smile
cool dude




PostPosted: Wed May 03, 2006 4:59 pm   Post subject: (No subject)

k i don't know if i should make new posts everytime because i'm just starting out and i have lots of questions so if u guys think its better to make new posts than i will! Smile

aside from that little note
this is my question

1)what is the difference between = and ==
2)in my if statement i tried saying:
code:

if (operation = "A"){
            sum = inum1 + inum2;
            System.out.println("the sum of the numbers is " + sum);
}


but this gives me an error saying incompatible types although i declared operation as string so it should be the same types right? the only way i could fix it is by saying

code:

 if (operation.equals ("A")){


can someone tell me why i can't use the equal sign (=)?

3) on error handling i have it working although if it is an error it says whatever message i want and then stops the program. how can i make it just go back and keep asking the question until they type something valid? would i need a loop for that?
wtd




PostPosted: Wed May 03, 2006 5:04 pm   Post subject: (No subject)

The = operator is used for assignment.

The == operator is used to check for equality in primitive types (int, float, boolean, double, long, etc.). In object types, it checks for referential equality. Do the two variables point to the same object in memory.

The equals methods checks for equality in object types.

Of what type is the "operation" variable?
Tony




PostPosted: Wed May 03, 2006 5:05 pm   Post subject: (No subject)

Edit: wtd is obviously a faster typer

re: = sign

It's used for assigning a value, as you do in
code:

sum = inum1 + inum2;

if is looking for a boolean operator to evaluate

in
code:

if (operation = "A"){

you assign the value of "A" to variable operator. Though the action does not return a boolean value.
code:

operation.equals ("A")

returns boolean true/false, something that if() understands.

== is used for comparison, though careful as you'll likely make the mistake of comparing one instance of a class to another. Even if they are of the same type and hold identical values, they will not be the "same" and you'll consistently get "false" for a result
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 3 of 4  [ 48 Posts ]
Goto page Previous  1, 2, 3, 4  Next
Jump to:   


Style:  
Search: