如何使用scanner检测两个字符串或int之间的换行?

rn0zuynd  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(440)

问题:
我在一个.txt文件中有这组数字,我想用 java.util.Scanner 检测中间的线路馈送 123 , 456 ,和 789 ,打印出换行符之间的数字,有什么办法吗?

1 2 3
// \n here
4 5 6
// \n here
7 8 9

输出: 456 ===========================================================================
我尝试过的解决方案:
(1) 我尝试使用hasnextline()方法,但是,hasnextline()似乎会告诉我下一行中是否有标记,并返回布尔值,而不是告诉我是否有标记\n。 if (scan.hasNextLine()) { \\ do something } (2) 我还尝试使用:(但是,使用这样的条件会说“syntax error on token'invalid character'”)

Scanner scan = new Scanner(new File("test.txt"));

// create int[] nums

while (scan.hasNext()) {
    String temp = scan.next();
    if (temp == \n) {
        // nums.add(); something like this
    }
}

System.out.print(nums); // something like this

我想用 \n 作为分隔符
另外,我做了google,大多数结果都告诉我使用.hasnextline(),但我希望它能识别换行符(\n)

gtlvzcf8

gtlvzcf81#

while (scanner.hasNext()) {
    String data = scanner.nextLine();
    System.out.println("Data: " + data);

    if (data.isEmpty()) {
        System.out.println("Found it");
        break;
    }
}
lyfkaqu1

lyfkaqu12#

Scanner 使用扫描下一个元素 new-line 或者 whitespace 默认情况下作为分隔符。让它阅读整个内容使用 scan.useDelimiter("\\Z") .

Scanner scan = new Scanner(new File("test.txt"));
scan.useDelimiter("\\Z");
final String content = scan.next();   // content: "1 2 3\r\n\r\n4 5 6"
int index = 0;
System.out.println("Index of \\n");
while (index != -1) {
     index = content.indexOf("\n", index);
     if (index != -1) {
          System.out.println(index);
          // Or do whatever you wish
          index++;
     }
}

输出:

Index of \n
5
7
niknxzdl

niknxzdl3#

我不确定我是否100%理解你的问题。所以我假设你的文件总是有两行被一行新行隔开( \n ). 如果我错了,请告诉我。

String charsAfterNewLine = null;

//try-catch block with resources
//Scanner need to be closed (scan.close()) after finish with it
//this kind of block will do `scan.close()` automatically
try(Scanner scan = new Scanner(new File("test.txt"))){

    //consume(skip) first line
    if(scan.hasNextLine()){
        scan.nextLine();
    }else{
        throw new Exception("File is empty");
    }

    //get second line
    if(scan.hasNextLine()){
        charsAfterNewLine = scan.nextLine();
    }else{
        throw new Exception("Missing second line");
    }

}catch(Exception e){
    e.printStackTrace();
}

System.out.println("charsAfterNewLine: " + charsAfterNewLine);

如果您想要简单的方式,无需尝试捕捉:

String charsAfterNewLine = null;

//throws FileNotFoundException
Scanner scan = new Scanner(new File("test.txt"));

if(scan.hasNextLine()){

    //consume(skip) first line
    scan.nextLine();

    if(scan.hasNextLine()){

        //get second line
        charsAfterNewLine = scan.nextLine();

    }else{
        System.out.println("Missing second line");
    }

}else{
    System.out.println("File is empty");
}

scan.close();

System.out.println("charsAfterNewLine: " + charsAfterNewLine);

结果(两者):

Input: 
    (empty file)
Output: 
    File is empty
    charsAfterNewLine: null

-----

Input:
    1 2 3 4 5 6
Output:
    Missing second line
    charsAfterNewLine: null

-----

Input:
    1 2 3\n4 5 6
Output:
    charsAfterNewLine: 4 5 6

相关问题