| Author |
Message |
cool dude

|
Posted: 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 if this works on your computers can u please tell me exactly which website u downloaded java platform 1.5? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
[Gandalf]

|
Posted: 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

|
Posted: Tue May 02, 2006 9:22 pm Post subject: (No subject) |
|
|
unforunately i can see i'm not going to be using your way. so back to using the old way which i will remember soon enough! 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  |
|
|
|
|
 |
cool dude

|
Posted: 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
|
Posted: 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.
Lastly: What happens if a user does enter something other than a valid number? |
|
|
|
|
 |
cool dude

|
Posted: 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.
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

|
Posted: 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) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
cool dude

|
Posted: 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

|
|
 |
wtd
|
Posted: 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

|
Posted: Tue May 02, 2006 10:29 pm Post subject: (No subject) |
|
|
finally figured out how to error proof that
| 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]

|
Posted: 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
|
Posted: Tue May 02, 2006 10:46 pm Post subject: (No subject) |
|
|
Perhaps you should read up on exception handling a bit more.  |
|
|
|
|
 |
cool dude

|
Posted: 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!
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
|
Posted: 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

|
Posted: 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 |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|