Results 1 to 5 of 5

Thread: Please Help!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    2

    Question 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.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52

    Exclamation

    Dilenger4, I disagree, you must use the == operator to check for a null-pointer.

    When firstNumber references a null-pointer, you can't check this with firstnumber.equals(null). Because firstNumber does not reference an object, you can't access the .equals method, so this results in a NullPointerException, just like the original problem.

    The correct code is:

    if (firstNumber == null || secondNumber == null) {
    System.out.println("Please supply both values");
    System.exit(0);
    }

    Mind the || instead of & - You don't want either number to contain a null-pointer.
    Last edited by hgroot; Jan 25th, 2002 at 11:50 AM.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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).

  5. #5
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    it's all-right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width