1
System.out.println("How many items did the customer purchase?");
numOfItem = scan.nextInt();
String numSuffix = "";
for (int i = 1; i \<= numOfItem; ++i) {
String numStr = Integer.toString(i);
if (numStr.length() \> 1 && numStr.charAt(numStr.length() - 2) == '1') {
    numSuffix = "th";
    System.out.println("Enter the " + i + numSuffix + " item name.");
    nameOfItem = scan.nextLine();
    } else if (numStr.charAt(numStr.length() - 1) == '1') {
    numSuffix = "st";
    System.out.println("Enter the " + i + numSuffix + " item name.");
    nameOfItem = scan.nextLine();
    } else if (numStr.charAt(numStr.length() - 1) == '2') {
    numSuffix = "nd";
    System.out.println("Enter the " + i + numSuffix + " item name.");
    nameOfItem = scan.nextLine();
    } else if (numStr.charAt(numStr.length() - 1) == '3') {
    numSuffix = "rd";
    System.out.println("Enter the " + i + numSuffix + " item name.");
    nameOfItem = scan.nextLine();
    } else {
    numSuffix = "th";
    System.out.println("Enter the " + i + numSuffix + " item name.");
    nameOfItem = scan.nextLine();
    }
    System.out.println("How much did this item cost?");
    totalPrice += scan.nextDouble();
    scan.nextLine();

It should be like : Enter the 1st item name. [user input] How much did this item cost? [user input]

But it always omit the input of the fist item's name like this: Enter the 1st item name. How much did this item cost? [user input]

enter image description here

invzbl3
  • 5,872
  • 9
  • 36
  • 76
Valery Yin
  • 11
  • 1
  • What's with the `\<=` and `\>`? That aside, your `numStr` check in the first `if` is badly formed, `length() - 2` is `-1` for a length-1 string like `"1"`. You can forgo the weird string operations and just use `i` directly. – Rogue Feb 17 '23 at 00:42

0 Answers0