【问题标题】:How do I check for \n when using java scanner?使用 java 扫描仪时如何检查 \n?
【发布时间】:2020-10-05 19:22:51
【问题描述】:

我正在尝试编写一个扫描器,以便每次检测到 \n 时,它都会扫描之后的行,直到出现新的 \n 为止。我第一次尝试这样的事情。

import java.util.Scanner;

public class test{
    
    public static void main(String[] args) {
        
        String input = "first line \nsecond line \nthird line";
        
        Scanner sc = new Scanner(input);
        
        while(sc.hasNextLine()) {
            String stuff = sc.nextLine();
            System.out.println(stuff);
        }
        sc.close();
    }
}

哪个有效,输出是

first line 
second line 
third line

但是,当我尝试使用 Scanner(System.in) 做同样的事情时,即使使用相同的输入,它也不会以同样的方式工作

import java.util.Scanner;

public class test{
    
    public static void main(String[] args) {
        System.out.println("Please enter things");
        
        Scanner cmd = new Scanner(System.in); //input: "first \n second \n third"
        
        String input = cmd.nextLine();
        
        Scanner sc = new Scanner(input);
        
        while(sc.hasNextLine()) {
            String stuff = sc.nextLine();
            System.out.println(stuff);
        }
        cmd.close();
        sc.close();
    }
}

输出:

first \n second \n third

我应该改变什么,以便每个 \n 都会打印一个新行?

编辑: 如果输入是

first
second
third

立即进入提示,scanner.nextLine() 就够了吗?

【问题讨论】:

  • 如果//input: "first \n second \n third" 表示您实际上是在控制台输入first \n second \n third,那么您的控制台会将\n 视为两个 单独的字符:\ 和@987654330 @ NOT 作为行分隔符。但是你想在这里做什么?如果您尝试提供 3 行,请像 line1 [press enter] line2 [press enter] line3 [press enter] 那样做(现在您需要决定什么应该停止循环,如果它在 N 之后停止行,或在用户提供的某些特定文本之后,例如“end”“bye”)。
  • @Pshemo 是的,我想一口气输入所有内容。我不知道\n 被视为两个字符,谢谢通知我!那么如果我同时输入它们,我将如何拆分first line \nsecond line \nthird line?我了解如何逐行输入命令,但这不是我想要在这里做的。
  • 不要这样输入。如果您的文本实际上将包含\n,其中应该代表两个字符,例如my file is at c:\my\new\folder?如果您想将大量数据传递给应用程序,那么可以将其存储在文件中,然后将该文件的位置作为参数传递?
  • @Pshemo 我明白你在说什么,但选择真的不取决于我:(我正在为作业写它,我们的教授说输入格式将是行分隔的通过换行符/返回变量。
  • 我不确定您所说的“输入格式中的“变量”将是由换行符/返回 变量 分隔的行”。无论如何,如果输入将提供实际的行分隔符,那么nextLine() 应该能够正确处理它。

标签: java java.util.scanner


【解决方案1】:
System.out.println("Please enter things");
    Scanner cmd = new Scanner(System.in); //input: "first \n second \n third"
    
    while(cmd.hasNext()) {
        String word = cmd.next();
        if(word.equals("\\n")) {
            System.out.println();
        }else {
            System.out.print(word);
            
        }
    }

【讨论】:

  • 所以这对于新程序员来说可能非常令人困惑,因为有两件事,首先 \n 代表 2 个字符而不是 1,这使得查找变得困难。其次,即使我忘记了 string==" " 无效,并且无论“”中的内容是什么,都会返回 false。确保你复习你的 String 方法,干得好!
  • 感谢您的帮助!有用!我肯定会花一些时间来玩弄它并自己理解它。好同事
  • 是的,您可能会改进的一件事是,如果每行有多个单词,则不会在单词之间放置空格,您可以在 else 语句中放置@987654323 来解决这个问题@
  • 请注意 next() 将返回 single 令牌,因此如果我们使用 a b c \n second 之类的输入,那么 System.out.print(word); a b c 将结束升级为abc
  • @Luke @Pshemo 我明白了,如果每一行用“enter”而不是“\n”分隔怎么办?如果输入是first (enter) second (enter) third?很难在 cmets 中输入,我将其添加到帖子中。会不会容易些?
【解决方案2】:

老实说,您需要在 while 循环之外的某个时间点使用这些子字符串,因此实际上最好根据相同的分隔符拆分行并让每个子字符串作为字符串数组中的一个元素这样你就不需要使用 Scanner 和 while 循环了,例如:

String input = "first line \n second line \n third line"; // Read in data file line...
String[] stuffArray = input.split("\\s+?\n\\s+?");
System.out.println(Arrays.toString(stuffArray));
System.out.println();
System.out.println("     OR in other words");
System.out.println();
for(String str : stuffArray) {
    System.out.println(str);
}

如果您想使用 System.in 执行此操作:

Scanner sc = new Scanner(System.in);
System.out.println("Enter text:");
String stuff = sc.nextLine();
String[] subArray = stuff.trim().split("(\\s+)?(\\\\n)(\\s+)?");
System.out.println();
    
// Display substrings...
for (String strg : subArray) {
    System.out.println(strg);
}

【讨论】:

  • 基于事实,“输出:first \n second \n third" we can assume that \n` 不是行分隔符,而是两个字符系列:\n(否则输出不会包含 \n 作为文本) 所以制作分隔符\n 不会影响它。所以它就像扫描字符串"first line \\nsecond line \\nthird line"
  • 我没用过分隔符,嗯,有意思!我对 Java 还很陌生,所以我仍在努力解决它,感谢您对我的教育!
  • @Pshemo 所以这不适用于Scanner(System.in)
  • @CaoTheNoob nextLine() 没有考虑 delimiter 所以即使你将它设置为 sc.useDelimiter("elephant"); 这个例子也会给你同样的结果。分隔符对于使用 next() nextInt() 等标记的其余方法很重要。
猜你喜欢
  • 2019-06-30
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 2012-07-11
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
相关资源
最近更新 更多