如何在使用Java扫描器时检查换行符?

huangapple go评论70阅读模式
英文:

How do I check for \n when using java scanner?

问题

我试图编写一个扫描器,以便每次检测到 \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() 足够吗?

英文:

I'm trying to write a scanner so that every time \n is detected, it will scan the line after that until a new \n shows up. I first tried something like this.

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();
	}
}

Which works, and the output is

first line 
second line 
third line

However, when I try doing the same thing with Scanner(System.in) it doesn't work the same way even with same input

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();
	}
}

Output:

first \n second \n third

What should I change, so that every \n will print a new line?

EDIT:
If the input was

first
second
third

and entered into the prompt at once, would scanner.nextLine() be enough to suffice?

答案1

得分: 1

System.out.println("请输入内容:");
Scanner cmd = new Scanner(System.in); // 输入: "第一个 \n 第二个 \n 第三个"

while(cmd.hasNext()) {
    String word = cmd.next();
    if(word.equals("\\n")) {
        System.out.println();
    } else {
        System.out.print(word);
    }
}
英文:
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);
    		
    	}
    }

答案2

得分: 0

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

String input = "first line \n second line \n third line"; // 读取数据文件行...
String[] stuffArray = input.split("\\s+?\n\\s+?");
System.out.println(Arrays.toString(stuffArray));
System.out.println();
System.out.println("     或者换句话说");
System.out.println();
for(String str : stuffArray) {
    System.out.println(str);
}

如果您想使用System.in来完成:

Scanner sc = new Scanner(System.in);
System.out.println("输入文本:");
String stuff = sc.nextLine();
String[] subArray = stuff.trim().split("(\\s+)?(\\\\n)(\\s+)?");
System.out.println();
    
// 显示子字符串...
for (String strg : subArray) {
    System.out.println(strg);
}
英文:

In all honesty, you will need to utilize these sub-strings at some point outside of your while loop so it would actually be better to split the line based on the same delimiter and have each substring as a element within a String Array This way you don't need to utilize Scanner and a while loop for this at all, for example:

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);
}

If you want to do this using 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);
}

huangapple
  • 本文由 发表于 2020年10月6日 03:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定