|
-
Jan 24th, 2002, 01:32 PM
#1
Thread Starter
New Member
Please Help!!!
I have just started studying java and am working on our first class assignment. It's just a simple program that takes two integers and tell which one is larger or if they're equal. What I want to know is what I need to put in the program to tell if they just pressed enter without entering any numbers. Because if they did do this the program just crashes. I think it's some sort of null thing, but i can't figure it out. Here's the code so far:
import javax.swing.JOptionPane;
public class Java12 {
public static void main( String args[] )
{
// Create the variables
String firstNumber,
secondNumber;
int number1 = 0,
number2 = 0,
largest = 0;
firstNumber =
JOptionPane.showInputDialog(" Enter the first integer");
secondNumber =
JOptionPane.showInputDialog(" Enter the second integer");
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// check to see which number is larger, smaller or equal
if (number1 > number2)
JOptionPane.showMessageDialog( null," " + number1 + " is larger " , "Results", JOptionPane.PLAIN_MESSAGE );
else
if (number2 > number1)
JOptionPane.showMessageDialog( null," " + number2 + " is larger " , "Results", JOptionPane.PLAIN_MESSAGE );
else
if (number2 == number1)
JOptionPane.showMessageDialog( null, "These numbers are equal ", "Results", JOptionPane.PLAIN_MESSAGE );
System.exit( 0 );
}
}
Last edited by Stussy_3; Jan 24th, 2002 at 01:55 PM.
-
Jan 24th, 2002, 10:39 PM
#2
Dazed Member
At the point in your code (which is below), firstNumber and secondNumber will contain String representations of the integers that the user has inputted so you could just test for null.
firstNumber =
JOptionPane.showInputDialog(" Enter the first integer");
secondNumber =
JOptionPane.showInputDialog(" Enter the second integer");
It just a matter of how you want to go about doing it.
You can use either
Code:
if(firstNumber.equals(null) || secondNumber.equals(null)){
System.out.println("Please supply bolth values");
}
or
Code:
if(!firstNumber.equals(null) | !secondNumber.equals(null)){
// code......
}else{
System.out.println("Please supply bolth values");
}
Just a note. You want to watch how you use test for equality. The "==" operator can be used to test whether two primative values are equal but when the "==" is applied to Object refrences(say two strings) then it tests for Object refrence equality not value. The "public boolean equals(Object obj)" method contained in the java.lang.String class which is inherited from the java.lang.Object class can be used to test for Object value equality.
Last edited by Dilenger4; Jan 25th, 2002 at 12:18 PM.
-
Jan 25th, 2002, 11:45 AM
#3
-
Jan 25th, 2002, 12:55 PM
#4
Dazed Member
Posted by hgroot
if (firstNumber == null || secondNumber == null)
yeah i thought about that last night using the || instead of & but i didnt want to go back downstairs and reboot. I guess if no input is recieved then either one or bolth of the strings would contain null values so maybe the .equals() method couldn't be invoked. I should have just used fresh code to test this instead of using his existing code(which i couldnt test because no interface was supplied other then two inputboxes).
Last edited by Dilenger4; Jan 27th, 2002 at 05:48 PM.
-
Jan 28th, 2002, 03:05 AM
#5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|