0

I should know how to do this but maybe I'm just tired. Anyways I need to write a simple while loop that asks the user how many of each item they want (with a Scanner). There are four items. The code currently looks as follows:

int item1Amount;
int item2Amount;
int item3Amount;
int item4Amount;
int c = 1;
while (x != -1) {
    System.out.print("Enter the number of Item " + c + " you would like to purchase: ");
    item1Amount = input.nextInt();
    c++;
}

The problem I am having in logic is that when it goes back through the loop it will be asking for Item 2, 3, and 4 but still entering the user input as item1Amount. How can this been done correctly?

5
  • 1
    Use an array instead Commented Sep 18, 2014 at 0:04
  • Declare and use an array. Or, even better, an ArrayList. Commented Sep 18, 2014 at 0:04
  • I would but technically I'm not supposed to know about arrays yet and I want to do this the right way for the chapter at hand. Commented Sep 18, 2014 at 0:05
  • 1
    Put in a switch. It will work but kinda ridiculous. Commented Sep 18, 2014 at 0:08
  • lol yes, the switch way is very ridiculous, but it does work. Unfortunately, switches don't exist yet either in this fictional world where I'm only on chapter 4. Commented Sep 18, 2014 at 0:26

4 Answers 4

2

With ArrayLists (Cleanest version):

ArrayList<Integer> itemAmounts = new ArrayList<Integer>();
int c = 1;
int x = 0;
while(x!=-1){
    System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
    int x = input.nextInt();
    itemAmounts.add(x);
    c++;
}

With Arrays (limited to exactly four items)

final int NUMB_ITEMS = 4;
int[] itemAmounts = new int[NUMB_ITEMS];
int c = 0;
int x = 0;
while(x!=-1 && c < NUMB_ITEMS){
    System.out.print("Enter the number of Item "+(c+1)+" you would like to purchase: ");
    int x = input.nextInt();
    itemAmounts[c] = x;
    c++;
}

With no data structure whatsoever:

int item1Amount;
int item2Amount;
int item3Amount;
int item4Amount;
int c = 1;
int a = 0;
while(x!=-1 && c<= 4){
    System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
    a=input.nextInt();
    switch(c){
        case 1:
            item1Amount = a;
            break;
        case 2:
            item2Amount = a;
            break;
        case 3:
            item3Amount = a;
            break;
        case 4:
            item4Amount = a;
            break;
        default:
            break;
    }
    c++;
}

With no data structure whatsoever AND no switches (ughhh... Chapter 4 land sucks):

int item1Amount;
int item2Amount;
int item3Amount;
int item4Amount;
int c = 1;
int a = 0;
while(x!=-1 && c<= 4){
    System.out.print("Enter the number of Item "+c+" you would like to purchase: ");
    a=input.nextInt();
    if(c == 1){
        item1Amount = a;
    } else if(c == 2){
        item2Amount = a;
    } else if (c == 3){
        item3Amount = a;
    } else if (c == 4) {
        item4Amount = a;
    }
    c++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow I never thought of using nested if statements. Nice going Mshnik. I'd give you mad reps if I was allowed to.
That's all a switch statement really is. It compares the value you give it to each case, if it matches, it enters the case statement. Else, it checks the next one, etc... Other than waterfall errors (unintentionally leaving out the break statement in a switch case), they do the same thing.
0

Move this line outside of the loop.

int c=1;

You're adding 1 to c and then overwriting it with 1 again.

You also should use an array to set the amount to purchase in an array instead of individual variables.

ArrayList itemAmounts = new ArrayList();

...

itemAmounts.add(input.nextInt());

1 Comment

True, but that still doesn't actually answer how to keep the counts for Item 1,2,3,4 separate from each other in the "input.nextInt()" phase.
0

It seems like you're using a pretty unorthodox method, but the easiest solution is to simply use an array of integers:

int[] arr = new int[4];
int c = 0;
while (c <= 4) { // loop until 4 values have been given
    System.out.print("Enter the number of Item " + (c + 1) + " you would like to purchase: ");
    arr[c] = input.nextInt();
    c++;
}

Printing c + 1 makes the output human readable, while keeping your array accession zero-indexed. I removed x from the loop condition, as it was not being used.

1 Comment

Yes this is the obvious way and the way I would do it. But I'm technically not supposed to know what an array is until chapter 6. (We're on chapter 4 right now). This is week 4 of an intro Java class. The only reason I personally know about arrays is past classes in Visual Basic. I'll probably just turn it in like this and my professor can suck it up.
0

Import your scanner and use an ArrayList. Within the loop add the element to the ArrayList with YourList.Add(YourElement);

If you want to use your variables, you can assign the value to the variable and then add it to your list. You'll just need to hard code your while loop to be while < 4

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.