2

Im having some trouble with part of a lab for class, most of it is easy but for some reason my while statement is not doing what I want it to. It's supposed to return invalid input, then prompt user to renter; however, it considers every input invalid. (Should be easy to see in code) How do I fix this? Or were could I find the info because I cant find it in my book. (The second while loop, first one works)

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class lab8
{
    public static void main (String[] args)
    {
        int choice;
        String item;
        double costper;
        ArrayList <String> Items = new ArrayList <String> (); 
        Items.add ("computer");
        Items.add ("Monitor");
        Items.add ("Color Printer");
        Items.add ("Color Scanner");
        Items.add ("DVD/CDROM");
        Items.add ("To Quit");

        ArrayList <Integer> Num = new ArrayList <Integer> ();
        Num.add (0);
        Num.add (1);
        Num.add (2);
        Num.add (3);
        Num.add (4);
        Num.add (5);

        System.out.println ("\t\tMy Super Computer Store\n");
        int index=0;
        int endex= 0;
        while (index < Items.size() && endex < Num.size())
        {
            System.out.println ("\t" +Num.get(index)+"\t"+Items.get (endex));
            index++;
            endex++;
        }
        Scanner scan = new Scanner (System.in);
        System.out.print ("\n\t\t\tEnter the item to purchase: ");
        choice = scan.nextInt ();
        {
            if (choice==5)
            {
                JOptionPane.showMessageDialog (null, "Thanks for considering My Super Computer Store");
                System.exit (0);
            }
        }
        {
            if (choice==0 || choice==1 || choice==2 || choice==3 || choice==4)
            {
                item = Items.get (choice);
            }
        }
        while (choice!=0 || choice!=1 || choice!=2 || choice!=3 || choice!=4 || choice!=5)
        {
            System.out.println ("Invalid Input, Please enter a integer between 0 and 5. ");
            System.out.print ("\n\t\t\tEnter the item to purchase: ");
            choice = scan.nextInt ();    
        }
        System.out.print ("\n\n\t\t\tEnter the quantity to purchase: ");
        int Qty = scan.nextInt ();
    }
}
2
  • Please use indentation, to make your code better readable. Commented Apr 11, 2012 at 2:35
  • Sorry about that... I forgot to actually review my changes before submitting the edit. Commented Apr 11, 2012 at 4:54

2 Answers 2

6

Your logic is wrong.

You want:

!(choice == 0 || choice == 1 || choice == 2 || choice == 3 || choice == 4)

which is not the same as what you wrote.

DeMorgan's Law is your friend :)

Note that by DeMorgan's Law

!(choice == 0 || choice == 1 || choice == 2 || choice == 3 || choice == 4)

is

(choice != 0 && choice != 1 && choice !=2 && choice !=3 && choice != 4)

And of course since you are using the integers you are for valid choices, you also could use the condition:

(choice < 0 || choice > 4)
Sign up to request clarification or add additional context in comments.

Comments

2

You've used || in your loop conditions. Logically, you're saying:

"If the choice isn't 0, or isn't 1, or isn't 2, etc.", meaning that a value of '1' fulfills the conditions because it's not 0, and it's not 2. Replace your while loop with:

while (choice!=0 && choice!=1 && choice!=2 && choice!=3 && choice!=4)

And you'll be ok.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.