java 扫描时如何忽略空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15005831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i ignore white space while scanning
提问by Aravind Datta
I have a file:
我有一个文件:
12345678;ABC 123456A12345678;45678945
This is what I do:
这就是我所做的:
Scanner s = new Scanner(new File(testCase.getFileName()));
while (s.hasNext()) {
String[] lineItems = s.next().split(";");
}
Output:
输出:
12345678;ABC
123456A12345678;
45678945
Desired output:
期望的输出:
12345678
ABC 123456A12345678
45678945
I want it to consider "ABC 123456A12345678"
as one single token and not break when it encounters whitespace.
我希望它被"ABC 123456A12345678"
视为一个单一的标记,并且在遇到空格时不会中断。
What should I do?
我该怎么办?
回答by Bernhard Barker
From here:
从这里:
"A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace".
“扫描仪使用分隔符模式将其输入分解为标记,默认情况下与空格匹配”。
So, your program splits the file according to whitespace first, and then .split(";");
splits these by ;
.
因此,您的程序首先根据空格拆分文件,然后.split(";");
按;
.
You need to set the delimiter to ;
as follows:
您需要将分隔符设置;
为如下:
Scanner s = new Scanner(new File(testCase.getFileName())));
s.useDelimiter(";");
while (s.hasNext()) {
System.out.println(s.next());
}
回答by Sarath Kumar Sivan
You can use something like this: String[] items = line.split(";");
你可以使用这样的东西: String[] items = line.split(";");
Consider the below example for better understanding: Let's assume that you have a file named “data.txt” located in your hard drive (say “C:/Users/sarath_sivan/Desktop”) which contains your records something like this:
考虑以下示例以更好地理解:让我们假设您的硬盘驱动器中有一个名为“data.txt”的文件(比如“C:/Users/sarath_sivan/Desktop”),其中包含您的记录,如下所示:
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
12345678;ABC 123456A12345678;45678945
And you would like to ignore white space while scanning with java.util.Scanner class.
并且您想在使用 java.util.Scanner 类进行扫描时忽略空白。
First, we can create a model class to holds your data. You can use something like this:
首先,我们可以创建一个模型类来保存您的数据。你可以使用这样的东西:
package com.stack.overflow.works.model;
/**
* @author sarath_sivan
*/
public class Data {
private String column1;
private String column2;
private String column3;
public Data() {}
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public String getColumn2() {
return column2;
}
public void setColumn2(String column2) {
this.column2 = column2;
}
public String getColumn3() {
return column3;
}
public void setColumn3(String column3) {
this.column3 = column3;
}
}
Next, we can create an interface for the scanning purpose. package com.stack.overflow.works.service;
接下来,我们可以创建一个用于扫描目的的界面。包 com.stack.overflow.works.service;
/**
* @author sarath_sivan
*/
public interface Scannable {
abstract public void scan();
}
Next, we can implement the business logic by creating a new class which implements the interface
接下来,我们可以通过创建一个实现接口的新类来实现业务逻辑
package com.stack.overflow.works.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.stack.overflow.works.model.Data;
/**
* @author sarath_sivan
*/
public class FileScanner implements Scannable {
private static final String SEMICOLON = ";";
private static final String TAB_SPACE = "\t";
private static final String FILE_NAME = "C:/Users/sarath_sivan/Desktop/data.txt";
@Override
public void scan() {
File file = new File(FILE_NAME);
Scanner scanner = null;
List<Data> recordList = new ArrayList<Data>();
Data data = null;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] items = line.split(SEMICOLON);
data = new Data();
data.setColumn1(items[0]);
data.setColumn2(items[1]);
data.setColumn3(items[2]);
recordList.add(data);
}
displayRecords(recordList); /*Displaying your records*/
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void displayRecords(List<Data> recordList) {
System.out.println("*DISPLAYING YOUR RECORDS:*");
System.out.println("COLUMN1" + TAB_SPACE + "COLUMN2" + TAB_SPACE + TAB_SPACE + "COLUMN3");
for (Data data: recordList) {
System.out.println(data.getColumn1() + TAB_SPACE + data.getColumn2() + TAB_SPACE + data.getColumn3());
}
}
}
Finally, we can create a service or test class to verify the logic. package com.stack.overflow.works.main;
最后,我们可以创建一个服务或测试类来验证逻辑。包 com.stack.overflow.works.main;
import com.stack.overflow.works.service.FileScanner;
import com.stack.overflow.works.service.Scannable;
/**
* @author sarath_sivan
*/
public class ScannerService {
public static void main(String[] args) {
Scannable fileScanner = new FileScanner();
fileScanner.scan();
}
}
Now, you can run the ScannerService class which produce the output something like this:
现在,您可以运行 ScannerService 类,它会产生如下输出:
DISPLAYING YOUR RECORDS:
显示您的记录:
COLUMN1 COLUMN2 COLUMN3
------------------------------------------
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
12345678 ABC 123456A12345678 45678945
You can see the package structure here
你可以在这里看到包结构
Hope this helps. Thank you!
希望这可以帮助。谢谢!
回答by user3392362
LinkedList<Double> a = new LinkedList<Double>();
File f = new File("C:/Users/etc.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
String [] tmp=null;
String line;
while ((line = in.readLine()) != null) {
if(line.trim().contains(" "))
tmp = line.split(" ");
a.add(Double.parseDouble(tmp[0].trim()));
}