0

So like in the title it works until it loops back and ask both questions and whatever input I give gives an error. I tried switching the while loop to a for loop but same thing.

import java.util.Scanner;

public class PS4Grocery {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String itemNum = "";
    int amount = 0;
    double total = 0;
    System.out.println(" Item Number |   Price  ");
    System.out.println("    A123     |  100.50");
    System.out.println("     A55     |   20.30");
    System.out.println("    B750     |   40.20\n");
    while (itemNum != "END") {
        System.out.println("Enter an item: ");
        itemNum = scan.nextLine();
        System.out.println("Enter a quantity: ");
        amount = scan.nextInt();
        if(itemNum=="A123") {
            total = total + (100.50 * amount);
        }
        else if(itemNum=="A55") {
            total = total + (20.30 * amount);
        }
        else if(itemNum=="B750") {
            total = total + (40.20 * amount);
        }
    }

    System.out.println("Total Price:      $" + total);

}

}

riley
  • 1
  • It keeps asking till the user enters END – riley Oct 04 '20 at 19:58
  • (1) You must not use `==` or `!=` to compare Strings. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. (2) `scan.nextLine()` will read until the end of the line, but `scan.nextInt()` will not. Use `scan.nextLine()` for both inputs, and parse the second input using [Integer.parseInt](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Integer.html#parseInt(java.lang.String)). – VGR Oct 04 '20 at 20:04

0 Answers0