4

The Program requires to interpret pipe delimited lines from a file and store it in two different ArrayLists. I have tried almost every related links on StackOverflow and came to know that | is a special operation and hence it is reserved. So I found several other ways to read the lines, but none of them actually works.

Here is my scenario:

The Text File looks like:

0|0.20
1|0.10
2|0.20

and so on...

The first Integer needs to go to ArrayList a1 and the second float number after the pipe delimiter need to go to ArrayList a2

I tried using scanner and them splitting the lines using split. Having saved them to a String and then doing the String conversion.I used following ways:

Method 1:

public static void readingFunc(String dirPath) throws  NumberFormatException, IOException{
    Path p = Paths.get(dirPath, "File.dat");
    for (String line: Files.readAllLines(p,StandardCharsets.US_ASCII)){
        for (String part : line.split("\\|")) {
            Integer i = Integer.valueOf(part);
            intArrayList1.add(i);
        }
    }

Method 2:

try(BufferedReader in = new BufferedReader(new FileReader(dirPath+"/File.dat"))){
          String line;
          while((line = in.readLine())!=null){
              String[] pair = line.split("\\|",-1);
              //a1.add(Integer.parseInt(pair[0]));
              //a2.add(Integer.parseInt(pair[1]))

Method 3:

try(BufferedReader in = new BufferedReader(new FileReader(dirPath+"/File.dat"))){
          String line;
          while((line = in.readLine())!=null){
              String[] pair = line.split("\\|",-1);

I also used several other Methods such as Scanner and couldn't get the result. I have several files similar to the one above and I need to read them to save it in an ArrayList for their processing.

PS: one of the file is having three data like:

1|0.21|0.37
2|0.08|0.12

and so on. I guess. this would be easy and similar to the two delimiter process. PS: I am developing on Linux Eclipse IDE so paths are:

/home/user/workspace1/Java_Code

I am sending the path as dirPath from the main function and then calling it here in a function. Please suggest me how to go with it?

I have checked already following Links:

Java Null value causing issue when reading a pipe delimited file

Read a file and split lines in Java.

Java - Scanner : reading line of record separated by Pipe | as delimiter

Java - putting comma separated integers from a text file into an array

Obtain number of token in a string with java Scanner

Community
  • 1
  • 1
Shubham agarwal
  • 157
  • 2
  • 3
  • 14
  • You should tell use what didn't work. And "method 2" and "method 3" are equal. – Tom Oct 29 '15 at 21:30
  • Hii Tom Thanks for your answer. Both of the method didn't work and I am not able to understand how can I get it. Can you spread some more briefing about it? Thanks four your quick response. – Shubham agarwal Oct 29 '15 at 21:34
  • Ok again: "didn't work" won't help anyone to understand where your problem is. For example method 2: what didn't work if you uncomment the last lines? – Tom Oct 29 '15 at 21:35
  • Hi Tom I tried to print the ArrayList and it is empty even though I saved using Method 1 and Method 2. This didn"T work. Do you think that regular expression is fine which I have used? – Shubham agarwal Oct 29 '15 at 21:39
  • 1
    I tried it too. Looks like it does work. The conclusion is that it is not doing what you want it to do. Therefore you need to describe more clearly what you want, with examples. – NickJ Oct 29 '15 at 21:39
  • Thanks guys. The problem was DOS and UNIX line ending. Answer from Janos below solved the issue. Thanks :) – Shubham agarwal Oct 29 '15 at 22:19

1 Answers1

1

You can specify the regex [|\n] as the delimiter for Scanner, for example:

Scanner scanner = new Scanner("0|0.20\n1|0.10\n2|0.20\n");
scanner.useDelimiter(Pattern.compile("[|\n]"));
System.out.println(scanner.nextInt());
System.out.println(scanner.nextDouble());
System.out.println(scanner.nextInt());
System.out.println(scanner.nextDouble());

To read the values into lists:

Scanner scanner = new Scanner("0|0.20\n1|0.10\n2|0.20\n");
scanner.useDelimiter(Pattern.compile("[|\n]"));

List<Integer> intList = new ArrayList<>();
List<Double> doubleList = new ArrayList<>();

while (scanner.hasNext()) {
    intList.add(scanner.nextInt());
    doubleList.add(scanner.nextDouble());
}
System.out.println(intList);
System.out.println(doubleList);

If the input file is DOS-formatted, then the delimiter pattern needs to be a bit more complicated, as the line ending is \r\n. This pattern will support both DOS and UNIX line endings:

scanner.useDelimiter(Pattern.compile("[|\n]|(\r\n)"));
janos
  • 120,954
  • 29
  • 226
  • 236
  • Well it is file that consists of such lines. I am trying with \n for the line break as well. PS: I guess, I need to put the File in the new Scanner parameter. – Shubham agarwal Oct 29 '15 at 21:37
  • I used a string for a reproducible demo. Yes, write `new Scanner(new File(dirPath+"/File.dat"))` and it should work – janos Oct 29 '15 at 21:43
  • Hii Janos Thank you very much for the response. I tried your code and got following error: Exception in thread "main" java.util.InputMismatchException – Shubham agarwal Oct 29 '15 at 22:00
  • The format is .dat and the file looks exactly the same as I shown. There is no \n at the line End. It is file having values like: 7|0.67 – Shubham agarwal Oct 29 '15 at 22:08
  • Ok, I think the problem is that your input file has DOS line endings. I updated my post, see the last paragraph. – janos Oct 29 '15 at 22:13
  • 1
    Super Cool! wow. What a great answer! I really appreciate your knowledge. :) Thanks a ton. I was fighting with this problem for almost 8 hours on my own. Thanks again!!!!!!! – Shubham agarwal Oct 29 '15 at 22:15
  • Just a question. Would this pattern also work on the file which looks like: 1|0.21|0.37 Of course, I will deal with the three Lists to deal the tokens on the lines. Thanks again. – Shubham agarwal Oct 29 '15 at 22:17
  • Yup, the pattern will work with that format too. You're welcome! – janos Oct 29 '15 at 22:18