【问题标题】:For or While loop skipping an input [duplicate]For或While循环跳过输入[重复]
【发布时间】:2012-09-29 23:19:54
【问题描述】:

可能重复:
Scanner issue when using nextLine after nextInt

我正在创建一个需要从我的服务器读取字符串和整数的客户端程序。根据接收到的整数,它会向 GUI 添加一些标签。到目前为止,我的程序读取了整数但跳过了字符串。当我尝试将整数写入程序时,以下输出是我的程序的输出:

  • 服务器写入:1
  • 服务器写入:1
  • 系统打印:1
  • 系统打印:j1
  • 系统打印:名称

问题是我无法编写字符串,因为它跳过了字符串。我怎样才能避免这个问题(注意我也试过for循环)

我的代码如下:

int times = client.reciveCommando();
int o = 0;
System.out.println(times);

while (o != times) {
  int j = client.reciveCommando();
  System.out.println("j"+ j);
  String name = client.reciveString();
  System.out.println("Name " +name);
  createUser(j, name);
  o++;

}

createUser 方法:

private void createUser(int j, String reciveChat) {
  if (j == 1) {
    chatPerson1.setVisible(true);
    lbl_Chatperson1_userName.setVisible(true);
    lbl_Chatperson1_userName.setText(reciveChat);
  } else if (j == 2) {
    lbl_chatPerson2.setVisible(true);
    lbl_userName2.setVisible(true);
    lbl_userName2.setText(reciveChat);
  } else {
    chatPerson3.setVisible(true);
    lbl_userName3.setVisible(true);
    lbl_userName3.setText(reciveChat);
  }
}

client.reciveCommando 方法:

public int reciveCommando() throws IOException{
  Integer i = input.nextInt();
  return i;
}

client.reciveString 方法:

public String reciveString(){
  String x = input.nextLine();
  return x;
}

希望有人能帮我解决这个问题:)

提前谢谢你。

【问题讨论】:

  • 你在哪里增加 o? (也许我是盲人?)如果 times == 0,您的 while 循环将跳过一个字符串。
  • 这可能是您正在寻找的,或者它可能对您有所帮助。 stackoverflow.com/questions/7056749/…

标签: java loops fencepost


【解决方案1】:

我在循环代码中没有看到您正在递增o 或更改times 的值。因此,要么完全跳过循环(即:times = 0),要么代码中的其他地方正在修改循环变量(o)或循环条件(times)——这两种情况下的编码都非常糟糕。

您的循环变量/增量规则在读取循环时应该非常清晰,并且可以轻松辨别开始/停止条件是什么,而无需读取可能在循环迭代期间修改值的其他方法/等。

我的直接猜测是times = 0,否则您将陷入无限循环。

【讨论】:

  • 好吧,我的复制粘贴在 while 循环结束时失败了,它确实是 o++;
  • 好吧,这基本上是这样的:第一个程序启动,然后它询问时间我将时间设置为 1(例如)然后它再次询问我另一个数字(这是客户的 id ) 我将其设置为 1。然后系统打印 1, j1 , name。如果我尝试设置一个字符串,我会收到一个错误,因为发布的东西不是一个整数(它指的是我reciveCommando),它是一个整数。
  • 我不关注。 “因为发布的东西不是一个整数(它把我引向了reciveCommando)”。发布了什么内容?
  • @int j = client.reciveCommando();程序要求一个整数然后程序跳到“System.out.Println("name "+ name);"并重复循环(将 o 设置为 +1),而不是向我询问字符串!所以基本上它会跳过这一行: String name = client.reciveString();
  • 什么是input?您是否尝试过使用调试器?单步执行每一行代码会发生什么?
【解决方案2】:

我找到了我的问题的解决方案,结果很简单!

首先让我解释一下我的意思。

当我的程序运行 while 循环时,它基本上跳过了应该从服务器接收输入的行。我发现它这样做的原因是 input.nextLine();是空的,当您阅读 input.nextLine() 的 api 时,这是有道理的;

将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。 由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索要跳过的行的输入。 回报: 被跳过的那一行

因为我试图获取的行是一个空行,它会跳过并将名称设置为“”。

这是我的程序的完整代码,目前可以运行:

while 循环:

            while (o != times) {
                int j = client.reciveCommando();
                System.out.println("j"+ j);
                    String name = client.reciveString();
                    System.out.println("Name " +name);
                    createUser(j, name);    
                o++;
            }

client.reciveString();

    public String reciveString(){

    String x = input.next();
    return x;
}

createUser();

    private void createUser(int j, String reciveChat) {
    if (j == 1) {
        chatPerson1.setVisible(true);
        lbl_Chatperson1_userName.setVisible(true);
        lbl_Chatperson1_userName.setText(reciveChat);

    }else if (j == 2) {
        lbl_chatPerson2.setVisible(true);
        lbl_userName2.setVisible(true);
        lbl_userName2.setText(reciveChat);
    }else if (j == 3){
        chatPerson3.setVisible(true);
        lbl_userName3.setVisible(true);
        lbl_userName3.setText(reciveChat);}

感谢您的所有回复,我一定会投票给您:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2014-03-23
    • 2014-07-30
    相关资源
    最近更新 更多