I'm not sue why this isn't working.
I am instantiating an object "list" from the Shopkeeper class. I'm trying to get the user to type in a specific item and then have a total that outputs the total of the items they purchased. (I'm learning Java on my own and believe I have the terminology correct.) Here is the code.
package dark.dominion;
import java.util.Scanner;
import java.lang.System;
public class Shopkeeper
{
public static void list(){
int rope = 1;
int bedroll = 3;
int rations = 2;
int total = 0;
String list = "";
System.out.println("Welcome to my shop. We have rope, a bedroll"
+ " and rations. Type the name of the items you want.");
Scanner keyboard = new Scanner(System.in);
list = keyboard.nextLine();
total = Integer.parseInt(list);
System.out.println("Your total is: " + total);
I am using netbeans and here are the error messages:
Exception in thread "main" java.lang.NumberFormatException: For input string: "bedroll"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at dark.dominion.Shopkeeper.list(Shopkeeper.java:31)
at dark.dominion.DarkDominion.main(DarkDominion.java: 44)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)
Thanks for any and all help!
Matt
Re: I'm not sue why this isn't working.
Quote:
NumberFormatException: For input string: "bedroll"
The parseInt() method doesn't think the String: "bedroll" has a vaid NumberFormat.
You should enter numeric digits and not letters.