0
所以我的程序运行良好,但是当我在项目用空格输入它抛出一个java.util.InputMismatchException。所以我改变了所有的kb.next()来kb.nextLine(),但与遇到的问题IM是例如:附加部分,只要它打印请输入你想添加标题。,它没有让我输入任何东西,它只是跳过到下一行并打印请输入有你想添加的项目的价值。Java的扫描仪串用空格
System.out.println("Please enter the title that you would like to add.");
title = kb.next();
System.out.println("Please enter the want value of the item that you would like to add.");
wantValue = kb.nextInt();
System.out.println("Please enter the have value of the item that you would like to add.");
haveValue = kb.nextInt();
。
package assignment3;
import java.util.Scanner;
public class Question1
{
static SortedList inventory;
//main
public static void main(String[] args)
{
inventory = new SortedList();
@SuppressWarnings("resource")
Scanner kb = new Scanner(System.in);
String command, title;
int wantValue, haveValue;
do
{
System.out.println("\n MAIN MENU");
System.out.println("H. Provide a summary of available commands");
System.out.println("L. List the entire inventory.");
System.out.println("I. Display Inventory information for a specific title.");
System.out.println("A. Add a new title to Inventory.");
System.out.println("M. Modify the want value of a specific title.");
System.out.println("S. Sell an item");
System.out.println("O. Write a purchase order for additional DVD's");
System.out.println("R. Write a return order.");
System.out.println("Q. Quit the program.");
command = kb.next();
//If the user need a summary of available commands.
if(command.equalsIgnoreCase("H"))
{
System.out.println("H (help), provides a summary of available commands.");
System.out.println("L (list), list the entire inventory.");
System.out.println("I<title> (inquire), Display theinventory information for a specific title.");
System.out.println("A<title> (add), add a new title to the existing inventory.");
System.out.println("M<title> (modify), modify the want value of a specific title.");
System.out.println("S<title> (sell), sell an item, decrease have value of that item by 1.");
System.out.println("O (order), write an order for additional items so have value = want value.");
System.out.println("R (return), return extras so have value = want value.");
System.out.println("Q (quit), quit the program.");
}
//If the user want the inventory info on a specific title
else if(command.equalsIgnoreCase("I"))
{
System.out.println("Please enter the title that you would like to inquire.");
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
//if a matching title is found
if((item.getTitle()).equals(title))
{
//print out the item
String s= item.toString();
System.out.println(s);
}
}
}
//If the user want a list the entire inventory.
else if(command.equalsIgnoreCase("L"))
{
//Search through the inventory
for(int i =1; i <=inventory.size(); i++)
{
StockItem item = (StockItem)inventory.get(i);
//print out the item
String s= item.toString();
System.out.println(s);
}
}
//If the user want to add a new item to the inventory
else if(command.equalsIgnoreCase("A"))
{
System.out.println("Please enter the title that you would like to add.");
title = kb.next();
System.out.println("Please enter the want value of the item that you would like to add.");
wantValue = kb.nextInt();
System.out.println("Please enter the have value of the item that you would like to add.");
haveValue = kb.nextInt();
//Add the new item to the inventory
StockItem NewItem = new StockItem(title,haveValue,wantValue);
inventory.sortedAdd(NewItem);
String s= NewItem.toString();
System.out.println(s);
}
//If the user want to change an item in the inventory
else if(command.equalsIgnoreCase("M"))
{
System.out.println("Please enter the title of the DVD you would like to modify");
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
//If an item is found, print it out and ask the user for a new want value
if((item.getTitle()).equals(title))
{
System.out.println("Title: " + item.getTitle());
System.out.println("Want value: " + item.getWant());
System.out.println("Have value: " + item.getHave());
System.out.println("Please enter the new want value for " + item.getTitle());
int newWantValue = kb.nextInt();
item.setWant(newWantValue);
String s= item.toString();
System.out.println(s);
}
}
}
//If the user want to sell an item for the inventory
else if(command.equalsIgnoreCase("S"))
{
System.out.println("Please enter the title of the DVD you would like to sell");
title = kb.next();
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
if((item.getTitle()).equals(title))
{
//if there is non in the inventory, add the user to the waiting list
if(item.getHave() == 0)
{
System.out.println("We are currently out of " + item.getTitle());
System.out.println("Please add customer to waiting list");
System.out.println("Please enter customer's last name");
String lastName = kb.next();
System.out.println("Please enter customer's first name");
String firstName = kb.next();
System.out.println("Please enter customer's phone number");
String phone = kb.next();
item.addToWaitingList(lastName,firstName,phone);
}
else{
//decrease the have by 1
item.setHave(item.getHave()-1);
}
String s= item.toString();
System.out.println(s);
}
}
}
//if the user want to print out a purchase order
else if(command.equalsIgnoreCase("O"))
{
//String s = " Purchase Order: \n";
System.out.println("Purchase Order:");
for(int i =1; i<=inventory.size(); i++)
{
StockItem item = (StockItem)inventory.get(i);
if(item.getHave()<item.getWant())
{
int ToBePurchased = item.getWant()+item.getSizeofWaitingList()- item.getHave();
String s = "\n Title: "+ item.getTitle() +"\t"+"Number in stock: "+ item.getHave()+"\t"+"Number of Wants:"+item.getWant()+"\t"+" Number of customers on waiting List : "
+item.getSizeofWaitingList()+"\t"+"Number to be Purchased: " +ToBePurchased ;
System.out.println(s);
}
}
}
//if the user want to a return order
else if(command.equalsIgnoreCase("R"))
{
//Search through the inventory
for(int i =1; i<=inventory.size();i++)
{
StockItem item = (StockItem)inventory.get(i);
if(item.getHave()>(item.getWant()+item.getSizeofWaitingList()))
{
int returnValue = item.getHave() - item.getWant() - item.getSizeofWaitingList();
item.setHave(returnValue);
String s= item.toString();
System.out.println(s);
}
if(item.getHave() == 0 && item.getWant() == 0 && item.getSizeofWaitingList() == 0)
{
inventory.sortedRemove(item);
String s= item.toString();
System.out.println(s);
}
}
}
} while (!command.equalsIgnoreCase("Q"));
}
}
添加一个断点,看看有什么title'变量'值当这种情况发生 –
我居然得到它通过创建一个新的输入扫描仪,需要用户输入的每个if语句后工作。 感谢你的想法太 – digitbizzGH