使用分隔符和hasNext在java中获取int用户输入?

时间:2017-03-28 22:05:51

标签: java string int java.util.scanner

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    in.useDelimiter(", "); // using delimiter ", "

    int i = 1;
    while(in.hasNextInt()){
        i = in.nextInt();
        System.out.println(i);
    }
}

我正在尝试从控制台获取输入,但此代码 not printing last integer 我怎么解决这个问题。我不想使用拆分并将结果保存在数组中。

2 个答案:

答案 0 :(得分:1)

您需要使用正则表达式模式。

import java.util.Scanner;
public class ApplicationProgram {

    static Scanner inKey = new Scanner(System.in);

    public static void main(String[] args) {
        TicketHandler Red = new TicketHandler (200,50);
        TicketHandler Green = new TicketHandler (200,50);
        TicketHandler Blue = new TicketHandler (200,50);
        TicketHandler Yellow = new TicketHandler (200,50);

        boolean running = true;

        //Main menu
        while (running) {
            printMenu();
            int KeyIn = inKey.nextInt();

            //menu
            switch (KeyIn) {
                case 1:{
                    RedRoute();
                    break;
                }
                case 2:{
                    GreenRoute();
                    break;
                }

                case 3:{
                    BlueRoute();
                    break;
                } 
                case 4:{
                    YellowRoute();
                    break;
                }

                case 5:{
                    ShowAvailableTicketsAndPrice();
                    break;
                }

                case 6:{
                    System.out.println("Do you wish to quit: type Y to confirm");
                    String quitYN = inKey.next ();
                    //System.out.println(quitYN);
                    if ((quitYN.matches("Y")) || (quitYN.matches("y"))) {
                        System.out.println("Goodbye\n\n");
                        running = false;
                        System.exit(0);

                    }
                    break;
                }
                default:{
                    System.out.println("Not a valid option, try again.");
                    break;
                }
            }
        }
    }

    public static void printMenu(){
        System.out.println("Bus Tickets");
        System.out.println("---- ----");
        System.out.println("1. \tRedRoute");
        System.out.println("2. \tGreenRoute");
        System.out.println("3. \tBlueRoute");
        System.out.println("4. \tYellowRoute");
        System.out.println("5. \tShowAvailableTicketsAndPrice");
        System.out.println("6. \tQuit");
    }

    public static void RedRoute(){
        Scanner Red = new Scanner(System.in);
        System.out.println();
        int count = 200;
        while (count <= 200) {
            System.out.println("Insert £" + count);
            int KeyIn = Red.nextInt();
            count = count - KeyIn;

            if (count == 0){
                System.out.println("Enjoy");
                break;
            }
        }
    }

    public static void GreenRoute(){

        Scanner Red = new Scanner(System.in);
        System.out.println();
        int count = 200;
        while (count <= 200){
            System.out.println("Insert £" + count);
            int KeyIn = Red.nextInt();
            count = count - KeyIn;

            if (count == 0){
                System.out.println("Enjoy");
                break;
            }
        }
    }   

    public static void BlueRoute(){

        Scanner Red = new Scanner(System.in);
        System.out.println();
        int moneyin = 200;
        while (moneyin <= 200){
            System.out.println("Insert £" + moneyin);
            int KeyIn = Red.nextInt();
            moneyin = moneyin - KeyIn;

            if (moneyin == 0){
                System.out.println("Enjoy");
                AmountOfTickets = get.AmountOfTickets - 1
                break;
            }
        }
    }

    public static void YellowRoute(){

        Scanner Red = new Scanner(System.in);
        System.out.println();
        int count = 200;
        while (count <= 200) {
            System.out.println("Insert £" + count);
            int KeyIn = Red.nextInt();
            count = count - KeyIn;

            if (count == 0){
                System.out.println("Enjoy");
                break;
            }
        }
    }

    public static void ShowAvailableTicketsAndPrice(){
        System.out.println("Routes available:");
        System.out.println("\tRed route, Tickets left: "+  "Price: ");
        System.out.println("\tBlue route, Tickets left: " + "Price: ");
        System.out.println("\tGreen route, Tickets left: " + "Price: ");
        System.out.println("\tYellow route, Tickets left: " + "Price: ");
    }
}

或者,如果它不一定用空格和逗号分隔(例如in.useDelimiter(",?\\s"); ):

1, 4,5465, 5

答案 1 :(得分:0)

使用Scanner.useDelimiter,最后一个输入整数之后的字符串应该与分隔符模式匹配,例如,如果您提供"1, 3, 5, 6, ",那么您将获得1 3 5 6作为输出,但如果您给出"1, 3, 5, 6"你不会得到最后一个整数。