【问题标题】:Java Scanner String inputJava 扫描仪字符串输入
【发布时间】:2011-08-23 10:07:43
【问题描述】:

我正在编写一个使用 Event 类的程序,其中包含一个日历实例和一个字符串类型的描述。创建事件的方法使用 Scanner 获取月、日、年、小时、分钟和描述。我遇到的问题是 Scanner.next() 方法只返回空格前的第一个单词。因此,如果输入是“我的生日”,那么对该事件实例的描述就是“我的”。

我做了一些研究,发现人们使用 Scanner.nextLine() 来解决这个问题,但是当我尝试这个时,它只是跳过了输入应该去的地方。这是我的代码的一部分:

System.out.print("Please enter the event description: ");
String input = scan.nextLine();
e.setDescription(input);
System.out.println("Event description" + e.description);
e.time.set(year, month-1, day, hour, min);
addEvent(e);
System.out.println("Event: "+ e.time.getTime());    

这是我得到的输出:

Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012

它跳过空格输入描述字符串,因此,描述(最初设置为空格 - “”)永远不会改变。

我该如何解决这个问题?

【问题讨论】:

  • 你没有打印描述,那么你怎么知道它没有正确阅读?
  • 你能包含一个输入示例吗?
  • 我不小心省略了打印描述的代码行,但我现在已经把它放进去了。 @Michael,我不知道要告诉你一个输入示例,因为我从来没有得到输入描述的提示。
  • 你是怎么输入时间信息的?

标签: java string java.util.scanner


【解决方案1】:

当您使用类似 nextInt() 的方式读取年月日小时分钟时,它会将其余行留在解析器/缓冲区中(即使它是空白的),因此当您调用 nextLine() 时,您正在阅读其余的第一行。

我建议您在打印下一个提示之前调用 scan.nextLine() 以丢弃该行的其余部分。

【讨论】:

  • 我也遇到了同样的问题。我还添加了scan.nextLine(); 代替input.nextLine(); ,但它给了我警告找不到符号scan 并创建扫描类。
  • @ZacharyDale 你只能使用你定义的变量。使用您定义的扫描仪。
  • @PeterLawrey 这是我的问题http://stackoverflow.com/questions/41882552/two-steps-running-at-once-in-java?noredirect=1#comment70947891_41882552
  • @ZacharyDale 您需要使用 nextLine () 来消耗单词或数字之后的剩余内容,即使您希望它为空。那么你需要再次调用nextLine()来获取后面的行。
【解决方案2】:
    Scanner ss = new Scanner(System.in);
    System.out.print("Enter the your Name : ");
    // Below Statement used for getting String including sentence
    String s = ss.nextLine(); 
   // Below Statement used for return the first word in the sentence
    String s = ss.next();

【讨论】:

    【解决方案3】:

    如果在 nextInt() 方法之后立即使用 nextLine() 方法,nextInt() 会读取整数标记;因此,该行整数输入的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数行的剩余部分(为空)。所以我们读取可以读取空白空间到另一个字符串可能会起作用。检查下面的代码。

    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            int i = scan.nextInt();
            Double d = scan.nextDouble();
            String f = scan.nextLine();
            String s = scan.nextLine();
    
    
            // Write your code here.
    
            System.out.println("String: " + s);
            System.out.println("Double: " + d);
             System.out.println("Int: " + i);
        }
    }
    

    【讨论】:

      【解决方案4】:
      import java.util.Scanner;
      
      public class Solution {
      
          public static void main(String[] args) {
              Scanner scan = new Scanner(System.in);
      
              int i = scan.nextInt();
              Double d = scan.nextDouble();
              scan.nextLine();
              String s = scan.nextLine();
              System.out.println("String: " + s);
              System.out.println("Double: " + d);
              System.out.println("Int: " + i);
          }
      }
      

      【讨论】:

        【解决方案5】:

        在扫描字符串之前使用它来清除先前的键盘缓冲区 它会解决你的问题 scanner.nextLine();//这是清除键盘缓冲区

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多