简体   繁体   English

带空格的Java扫描器字符串

[英]Java scanner string with spaces

So my program runs fine but when i type in items with spaces its throws a java.util.InputMismatchException. 因此,我的程序运行良好,但是当我输入带空格的项目时,它会抛出java.util.InputMismatchException。 so i changed all the kb.next() to kb.nextLine() but the problem im having with that is for example: the add section, as soon as it prints Please enter the title that you would like to add. 所以我将所有kb.next()都更改为kb.nextLine(),但是例如,我所遇到的问题是:add部分,在打印后立即输入您要添加的标题。 , it doesnt let me input anything, it just skips to the next line and prints Please enter the have value of the item that you would like to add. ,它不能让我输入任何内容,只是跳到下一行并打印请输入您要添加的项目的具有值。

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"));

    }
}

I got it, i added a new input scanner Scanner input = new Scanner(System.in); 我知道了,我添加了一个新的输入扫描仪Scanner input = new Scanner(System.in); after every if statement that requires a user's input. 在每个需要用户输入的if语句之后。 instead of having a global one. 而不是拥有一个全球的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM