Java,程序不会停止进行scan.nextLine() [英] java, programm not stopping for scan.nextLine()

查看:60
本文介绍了Java,程序不会停止进行scan.nextLine()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,它停下来让我输入服务器名和它们的端口号,此后它应该停下来让我输入用户名,但它永远不会停下来进行scan.nextline()并直飞输入接收者名称??

When I run the following code it stops to let me enter servername and them port number, after that it is supposed to stop and let me enter username but it never pauses for scan.nextline() and flies straight to enter reciever name??

public static void main(String[] args) throws IOException {
    try {
        System.out.println("\n\n\nTCP Chat Client\n\nEnter server name:");
        Scanner scan = new Scanner(System.in);

        //get server info from user
        serverName = scan.nextLine();

        System.out.println("\nEnter port number:");
        serverPort = scan.nextInt();


        System.out.println("Enter your username:\n");
        userName = scan.nextLine();

        //make connection to server
        cSocket = new Socket(serverName, serverPort);
        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

        //send username to server
        out.println(userName);
        String rcvrname="wefwef";
        String message=null;
        //start messaging
        while(!(rcvrname.equals("exit"))){
            System.out.println("Enter reciever name");
            out.println(scan.nextLine());
            System.out.println("Enter message");
            out.println(scan.nextLine());

        }
        out.close();
        in.close();
        cSocket.close();

    }

推荐答案

我将再次使用 scan.nextLine(),而不是 scan.nextInt()然后解析输入.如果得到 NumberFormatException ,则可以向用户咆哮说输入已被插入.

Instead of scan.nextInt(), I would use scan.nextLine() again, and then parse the input. If you get a NumberFormatException, you can bark at the user that the input was invlaid.

之所以需要执行类似的操作,是因为在serverPort输入的末尾仍然有一个换行符.当您使用 nextInt()时,扫描程序将获取所有下一个令牌,并尝试将其转换为 Integer .Integer令牌之后的所有内容都需要扫描.

The reason why you need to do something like this is that you still have a newline sitting at the end of your serverPort input. When you use nextInt(), the scanner grabs all of the next token and tries to convert it into an Integer. Anything after the Integer token remains to be scanned.

为了证明这一点,如果使用原始代码,则在服务器端口输入之后放置一个令牌分隔符(如空格),然后添加一些文本,您将发现随后的 userName 分配将从数字末尾移至下一个换行符.

To prove this, with your original code, if you put a token separator, like a space, after your server port input and then add some text, you will find that your subsequent userName assignment will grab from the end of the number to the next newline.

    System.out.println("\nEnter port number:");
    Integer serverPort = scan.nextInt();
    System.out.println("\nEnter your username:");
    String userName = scan.nextLine();

    System.out.println("\nserverPort: " + serverPort 
                     + "\nuserName: " + userName);

输入以下内容:

    Enter port number: 
    123 abc
    Enter your username:

将输出

   serverPort: 123
   userName:  abc

这是因为该空格充当令牌分隔符,并且在调用 scan.nextInt()之后,空格和 abc 仍保留在缓冲.调用随后的 scan.nextLine()时,将从缓冲区中扫描空格和 abc .

This is because the space is acting as a token separator, and after the scan.nextInt() is called, the space and the abc are still left in the buffer. When the subsequent scan.nextLine() is called, the space and the abc are scanned from the buffer.

这篇关于Java,程序不会停止进行scan.nextLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆