2

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.

What should I do?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Aravind Datta
  • 23
  • 1
  • 1
  • 3

3 Answers3

6

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

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());
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

You can use something like this: 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:

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.

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;

/**
 * @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;

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:

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 enter image description here

Hope this helps. Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1218985
  • 7,531
  • 2
  • 25
  • 31
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()));
    }
user3392362
  • 356
  • 4
  • 11