Determining Item Numbers Using If and Else

Utilizes If, Else, and the OR operator

Page 1 of 1

2 Replies - 2046 Views - Last Post: 30 October 2010 - 11:02 AM Rate Topic: -----

#1 GamingDoc   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Determining Item Numbers Using If and Else

Posted 30 October 2010 - 08:29 AM

Greetings again, I have one more question to ask.

I'm on this debugging exercise that determines if an item number is invalid and if it is valid, then it would fall into two categories: Automotives or Housewares. Any item number over 999 is considered invalid, as well as any number below 111. The user is supposed to input an item number and to have one of the following three output: Automotive Department, Housewares Department, or invalid. The original exercise is as shown:

// DebugFive3.java
// Determines whether item number on order is valid
// Over 999 invalid
// Less than 111 Invalid
// Valid and less than 500 - Automotive Department
// Valid and 500 or higher Housewares Department
import javax.swing.*;
public class DebugFive3
{
   public static void main (String args[])
   {
      int item;
      String itemString, output;
      itemSting = JOptionPane.showInputDialog(null,
         "Please enter item number
      item = Integer.parseInt(itemString);
      if(item < 111)
         output = "Item number too low";
      else
        output = "Item number too high";
        else
          if(item < 500)
               output = "Valid - in Automotive Department";
          else
               output = "Valid - Item in Housewares Department";
       JOptionPane.showMessageDialog(output);
       System.exit(0);
   }
}




The first thing I did was to combine the two if out out statements so that if a number was invalid, then only one statement would be needed to evaluate that. My fixed version so far is this:

// Alphonsus Delgra
// October 27, 2010
// Lab 5-1
// DebugFive3.java
// Determines whether item number on order is valid
// Over 999 invalid
// Less than 111 Invalid
// Valid and less than 500 - Automotive Department
// Valid and 500 or higher Housewares Department
import javax.swing.*;
public class FixDebugFive3
{
   public static void main (String args[])
   {
      int item;
      String itemString, output;
      itemString = JOptionPane.showInputDialog(null,
         "Please enter item number: ");
      item = Integer.parseInt(itemString);
      if(item < 111 || item > 999)
         output = "Invalid item number (too low or too high";
        else
          if(item < 500)
               output = "Valid - in Automotive Department";
          else
               output = "Valid - Item in Housewares Department";
       JOptionPane.showMessageDialog(output);
       System.exit(0);
   }
}



The only problem I had when compiling was this:

 ----jGRASP exec: javac -g H:\Java 2nd\FixDebugFive3.java

FixDebugFive3.java:27: cannot find symbol
symbol  : method showMessageDialog(java.lang.String)
location: class javax.swing.JOptionPane
       JOptionPane.showMessageDialog(output);
                  ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



Without a symbol I don't know what's wrong with that. I checked towards the beginning if I used the correct import with javax.swing.* but that imports all Java packages. Checking through spelling errors other details it looked fine to me. Thanks again for you time.

Is This A Good Question/Topic? 0
  • +

Replies To: Determining Item Numbers Using If and Else

#2 H3R3T1C   User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Determining Item Numbers Using If and Else

Posted 30 October 2010 - 09:58 AM

Your problem is that JOptionPane.showMessageDialog() takes 2 params (Component parentComponent, Object message)
This should fix your code:
JOptionPane.showMessageDialog(null,output);


The reason I put null there is that there is no parent Component so if null it will just center the JOptionPane.
Was This Post Helpful? 1
  • +
  • -

#3 GamingDoc   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Determining Item Numbers Using If and Else

Posted 30 October 2010 - 11:02 AM

Thanks a lot H3R3T1C, it works just fine now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1