我如何在扫描时忽略空白

问题描述 投票:2回答:3

我有一个文件:

12345678;ABC 123456A12345678;45678945

这是我的工作:

Scanner s = new Scanner(new File(testCase.getFileName()));
while (s.hasNext()) {
    String[] lineItems = s.next().split(";");
}

输出:

12345678;ABC
123456A12345678;
45678945

所需的输出:

12345678
ABC 123456A12345678
45678945

我希望它将"ABC 123456A12345678"视为一个单独的令牌,并且在遇到空白时不会中断。

我该怎么办?

java whitespace token java.util.scanner
3个回答
6
投票

来自here

“扫描程序使用定界符模式将其输入分成令牌,默认情况下与空格匹配。”

因此,您的程序首先根据空白分割文件,然后.split(";");;分割文件。

您需要将定界符设置为;,如下所示:

Scanner s = new Scanner(new File(testCase.getFileName())));
s.useDelimiter(";");
while (s.hasNext()) {
   System.out.println(s.next());
}

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

0
投票

您可以使用类似以下的内容:String[] items = line.split(";");

请考虑以下示例以更好地理解:假设您在硬盘驱动器中有一个名为“ 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

并且您想在使用java.util.Scanner类进行扫描时忽略空白。

首先,我们可以创建一个模型类来保存您的数据。您可以使用类似这样的内容:

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

}

接下来,我们可以创建一个用于扫描目的的界面。包com.stack.overflow.works.service;

/**
 * @author sarath_sivan
 */

public interface Scannable {

    abstract public void scan();

}

接下来,我们可以通过创建一个实现该接口的新类来实现业务逻辑。>

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

}

最后,我们可以创建服务或测试类以验证逻辑。包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();
    }

}

现在,您可以运行ScannerService类,该类产生如下所示的输出:

显示您的记录:

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

您可以在此处看到包结构“在此处输入图像描述”>>

希望这会有所帮助。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.