9 Replies Latest reply on Aug 22, 2003 7:55 PM by 807595

    do while and if else

    807595
      Hey, I'm in a Java class at school, and we just learned do while and if else statements. Here is the relevant code:
                String strNum;
                double number1;
                
                do
                {
                     strNum=JOptionPane.showInputDialog("Enter any number between zero and two-hundred-fifty:");
                     number1=Double.parseDouble(strNum);
      
                          if (strNum.equalsIgnoreCase("quit") == true)
                          {
                               JOptionPane.showMessageDialog(null, "Press Ok to confirm...", "Aww..do you have to?", JOptionPane.PLAIN_MESSAGE);
                               System.exit(0);
                          }
                          if (number1<=250 == true)
                          {
                               if (number1>=0 == true)
                               {     
                                    JOptionPane.showMessageDialog(null, "You have entered a valid entry!", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);
                                    System.exit(0);
                               }
                               else
                               {
                                    JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                                    System.exit(0);
                               }
                          }
                          else 
                          {
                               JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                               System.exit(0);
                          }
                }
                while (number1<=250 == true);
      Now, my question is this: why doesn't it display a message dialog when I type in quit (not case sensitive) in the input dialog? It instead says exception in main thread... I tried making a char variable to equal the literal quit, and then make an if statement such as: if (strNum.equalsIgnoreCase(confirm1) == true), but it didn't work. Any thoughts? Is there any other way to do this? Thanks!
        • 1. Re: do while and if else
          807595
          Two things.

          All this "== true" stuff in your code is pointless and bad style. It's added work for no gain. If the == expression would evaluate to true, then the expression to the left side of the == is true, and that's sufficient for the logic test.

          Secondly, you say:
          "It instead says exception in main thread..."
          I'm sure it says a lot more than just that. Error messages are there to help you. Don't just stop when you see an error and then ignore the rest of it. Read it to find out what the problem is.
          • 2. Re: do while and if else
            807595
            You must first check for the "quit" before trying to parse the double. Mentioning trying, you should use a try-catch around that parseDouble, since lots of other string could fail to parse.

            When you get an exception take a careful look at all that stuff it displays, there are often very useful hints in the dump.

            • 3. Re: do while and if else
              807595
              Two things.
              All this "== true" stuff in your code is pointless and bad style. It's added work for no gain. If the == expression would evaluate to true, then the expression to the left side of the == is true, and that's sufficient for the logic test.Well, I wanted it to work before I optimized the code...
              >
              Secondly, you say:
              "It instead says exception in main thread..."
              I'm sure it says a lot more than just that. Error messages are there to help you. Don't just stop when you see an error and then ignore the rest of it. Read it to find out what the problem is.I did read the rest, I just didn't bother to type it all. It looks jumbled to me - here is all of it :
              Exception in thread "main" java.lang.NumberFormatException: For input string: "quit"
                   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                   at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1213)
                   at java.langDouble.parseDouble(Double.java:202)
                   at ProjTwo.main(ProjTwo.java:12)
              Any idea what this means?

              pkwooster: What do you mean use a try-catch? I'm unfamiliar with that term. Thanks :)
              • 4. Re: do while and if else
                807595
                All this "== true" stuff
                Well, I wanted it to work before I optimized the code...
                It's not an optimization. It's keeping your code clean and logical so you can get it to work.
                I did read the rest, I just didn't bother to type it all. It
                looks jumbled to me
                It's not jumbled. The first line tells you what the problem is, and in what thread. The rest shows you the call stack (the method invocations in the order they were called from bottom to top, so the top line shows you were in the code it happened.

                Your prof should actually have gone over this the first day. If you can't read, interpret, and deal with error messages, how are you ever going to learn?

                Continued next post...
                • 5. Re: do while and if else
                  807595
                  Exception in thread "main" java.lang.NumberFormatException: For input string: "quit"
                  There was a NumberFormatException. You can look that up in the API, but you can see what it means: a problem with the format of a number. It tried to read a number in a String, but found garbage instead. It even shows you the garbage: "quit", which of course isn't a number.
                  at java.lang.NumberFormatException.forInputString(NumberFomatException.java:48)
                  at java.lang.FloatingDecimal.readJavaFormatString(Floatingecimal.java:1213)
                  at java.langDouble.parseDouble(Double.java:202)
                  This detected the error and created the NumberFormatException. You can see (in the parentheses) the source code files and line numbers for the code that is being run at that point. These are in standard java classes, so they're probably not the problem. Look for data about your code, which is where the trouble is.
                  at ProjTwo.main(ProjTwo.java:12)
                  Aha! This is your program, and it's saying that there's a problem on line 12. This is where it was trying to parse a number, but failed.
                  • 6. Re: do while and if else
                    807595
                    It's not jumbled. The first line tells you what the
                    problem is, and in what thread. The rest shows you
                    the call stack (the method invocations in the order
                    they were called from bottom to top, so the top line
                    shows you were in the code it happened.
                    Let me clarify that. The top line shows you the problem and thread.
                    The second to top line shows the innermost method, the one that threw the exception. The line beneath that shows the code that calls the line above it.

                    So if a() calls b() calls c() which throws an Exception, you'll see something like:
                    Exception in thread "main": SomeException
                      at mypackage.MyClass.c() (MyClass.java:8)
                      at mypackage.MyClass.b() (MyClass.java:33)
                      at mypackage.MyClass.a() (MyClass.java:93)
                    It goes on, often all the way to the main() method.
                    • 7. Re: do while and if else
                      807595
                      I took the liberty of editing your code to insert a try-catch block that should do the trick and help you avoid the exception, although i'd sincerely advise u brush up on the points mentioned by the others who've posted before so that u get a good grasp on the Control flow and exception handling - that should help you get upto speed with basic java programming.

                      do
                      {
                      strNum=JOptionPane.showInputDialog("Enter any number between zero and two-hundred-fifty:");
                      try{                                                        number1=Double.parseDouble(strNum);

                      if (number1<=250 == true)
                      {
                      if (number1>=0 == true)
                      {      
                      JOptionPane.showMessageDialog(null, "You have entered a valid entry!", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);
                      System.exit(0);
                      }
                      else
                      {
                      JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                      System.exit(0);
                      }
                      }
                      else
                      {
                      JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                      System.exit(0);
                      }

                      }//end of try
                      catch(NumberFormatException ne){
                      if (strNum.equalsIgnoreCase("quit") == true)
                      {
                      JOptionPane.showMessageDialog(null, "Press Ok to confirm...", "Aww..do you have to?", JOptionPane.PLAIN_MESSAGE);
                      System.exit(0);
                      }
                      }//end of catch block

                      }
                      while (number1<=250 == true);

                      Hope the above helps,

                      John Morrison
                      • 8. Re: do while and if else
                        807595
                        Thanks for all the help! My prof helped me out with it, and found out that it was trying to look for a number in "quit", and since it didn't find any it had a runtime error. Here is my updated code for anyone who is curious:
                        (I also changed a few things)
                        import javax.swing.JOptionPane;
                        public class ProjTwo
                        {
                             public static void main(String[] args)
                             {
                                  String strNum=new String();
                                  double number1;
                                  int in1=0;
                                  int on1=0;
                                  
                                  while (in1 == on1)
                                  {
                                            strNum=JOptionPane.showInputDialog("Enter any number between zero and two-hundred-fifty:");
                        
                                            if (strNum.equalsIgnoreCase("quit"))
                                            {
                                                 JOptionPane.showMessageDialog(null, "Press Ok to confirm...", "Aww..do you have to?", JOptionPane.PLAIN_MESSAGE);
                                                 System.exit(0);
                                            }
                                            number1=Double.parseDouble(strNum);
                                            if (number1<=250)
                                            {
                                                 if (number1>=0)
                                                 {     
                                                      JOptionPane.showMessageDialog(null, "You have entered a valid entry!", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);
                        
                                                 }
                                                 else
                                                 {
                                                      JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                                                 }
                                            }
                                            else 
                                            {
                                                 JOptionPane.showMessageDialog(null, "You have entered an invalid entry!", "Shame on you!", JOptionPane.ERROR_MESSAGE);
                                            }
                                  }
                             }
                        }
                        • 9. Re: do while and if else
                          807595
                          while (in1 == on1)
                          I also made a runaround just to put some boolean value true in "while" as I was in a hurry to finish it in class.