0

I want to make a program that asks the user to enter how many laptops they want to buy, then ask them if the laptops have different prices. if the answer is "yes", it's gonna repeat the question based on the laptop's quantity. Otherwise it'll just ask once and then calculate the cost of the laptop/s

I get this:

How many laptops do you want to buy? 3
Do they have different price (yes/no)? Enter laptop/s price:
import java.util.Scanner;

public class LaptopCostCalculatorV2 {
    public static void main(String[] args) {
        int laptopsPrice = 0;
        int laptopQuantity;

        Scanner scan = new Scanner(System.in);

        System.out.print("How many laptops do you want to buy? ");
        laptopQuantity = scan.nextInt();
        System.out.print("Do they have different price (yes/no)? ");
        String choose = scan.nextLine();
        if (choose.equals("yes")) {
            for (int i = 0; i < laptopQuantity; i++) {
                System.out.println("Enter laptop price");
                laptopsPrice = scan.nextInt();
            }
        } else {
            System.out.println("Enter laptop/s price: ");
            int laptopPrice = scan.nextInt();
        }
    }
}

Why does it not let me enter the answer to the second question?

Laurenz Albe
  • 129,316
  • 15
  • 96
  • 132
A A
  • 11
  • 1

1 Answers1

0
import java.util.Scanner;

public class LaptopCostCalculatorV2 {
    public static void main(String[] args) {
        int laptopsPrice = 0;
        int laptopQuantity;

        Scanner scan = new Scanner(System.in);

        System.out.print("How many laptops do you want to buy? ");
        laptopQuantity = scan.nextInt();
        scan.nextLine();
        System.out.print("Do they have different price (yes/no)? ");
        String choose = scan.nextLine();
        if ("yes".equals(choose)) {
            for (int i = 0; i < laptopQuantity; i++) {
                System.out.println("Enter laptop price");
                laptopsPrice += scan.nextInt();
                scan.nextLine();
            }
        } else {
            System.out.println("Enter laptop/s price: ");
            laptopsPrice += scan.nextInt() * laptopQuantity;
            scan.nextLine();
        }
        System.out.println("Total is: " + laptopsPrice);
    }
}

nextInt reads the next integer in buffer but does not "skip" to the next line automatically. This behavior lets you read multuple integers in one line with consecutive calls to nextInt(), but to start reading the next line a call to nextLine() should follow. nextLine() actually skips beyond the newline and returns whatever was remaining before it. Since we're not interested in reading the rest, we just discard its return value. If 12 whatever was the line then 12 would be the result of nextInt(), and " whatever" would be that of following nextLine(). Even if the user only typed 12, the nextLine() is required although it would simply return an empty string "".

Jin-oh Kang
  • 307
  • 1
  • 8
  • thanks, can you tell me what this mean? scan.nextLine(); – A A Jan 26 '20 at 15:16
  • To the downvoter: I've since proofread and corrected the wrong identifier and missing multiplication by the quantity; I didn't really check if my code compiles and is correct at first since I supposed OP was just asking about the subtle Scanner parsing behaviour and demonstrating how `nextLine()` might work here would be enough. Rest could be figured out easily. – Jin-oh Kang Jan 26 '20 at 15:28