-3

In this line I need to input the following "MacBook Pro" however it brings up and error. This is the code in question.

 System.out.print("What brand of computer do you wish to purchase? "); // user prompt
    model = scanner.next(); // read input here as a string

This is the following error

Exception in thread "main" java.lang.NumberFormatException: For input string: "pro"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at p2code.Computer.main(Computer.java:22)

Declaration added for clarification

Scanner scanner = new Scanner(System.in);   // Declare a Scanner object here to be used for input
    String input;
    String brand;
    String model;

Lines 20-23 below

    System.out.print("Enter the cost of the computer: "); //user prompt
        input=scanner.next(); //reads input as string 
        price = Double.parseDouble(input); // converts string to double and stores in price variable
    totalTax = (price * kTax);
Narcosed
  • 1
  • 2
  • Can you post the declaration part of the code. Seems like model is declared with Double – Jagadesh Jun 17 '20 at 06:05
  • as the exception refers to line 22 in your code (Computer.java:22) - > show us that line plus a few before and after :-) – Jan Jun 17 '20 at 06:06

1 Answers1

0

you need to use nextLine() to input the "Macbook Pro".
next() only takes a single word as input.
So pro is left and error is thrown.

ProGamer
  • 380
  • 3
  • 16