0

I'm currently stumped to how I can read from a text file whilst removing all whitespace. I have tried using the delimiter and string split to find no success. The problem is: my task requires me to read several lines of text from a text file and store it in an array.

However one of the lines has a visible 'gap' or several whitespaces (denoted by the speech marks here) which I assume is causing me problems.

Here is a sample of the text file:

jy1 10 10 10 10 10

mp1 6 5 6 " " 7 5

ls1 9 7 12 14 15

mp2 10 12 15 15 14

lb1 12 12 12 12 12

And here is my code:

try {
    FileReader reader= new FileReader(attendancesFile.txt);
    String line="";
    Scanner in=new Scanner(reader);

// reading from file line by line
    while (in.hasNextLine())    {
        line=in.nextLine();
        // splitting line read in separate words
        String tokens[]=line.split(" ");

        // creates an array of attendances 
        // using 5 separate attendances represented as integer values from file
        String id= tokens [0];
        int attend1= Integer.parseInt(tokens [1]);
        int attend2= Integer.parseInt(tokens [2]);
        int attend3= Integer.parseInt(tokens [3]);
        int attend4= Integer.parseInt(tokens [4]);
        int attend5= Integer.parseInt(tokens [5]);
        int [] attend= {attend1, attend2, attend3, attend4, attend5};

        // uses FP method to find Fitness Class with given ID
        // sets the attendance for Fitness Class with given ID
        fp.classByID(id).setAttendance(attend);
    } 
    reader.close();
    in.close();
}   
catch (IOException e)   {
    System.err.println("Reading from file error.");
}

Ideally I should have 5 arrays, each of which stores the 5 integer numbers from each line of the file. I will then pass these arrays to another class where a method adds this array of int to an object array.

However, it gives me an error: Exception in thread "main" java.lang.NumberFormatException: For input string: "" which I assume is due to the white space being read and added to the array. I have tested to print out each array & it appears as though the fourth array only has 4 elements & a blank space.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Kite
  • 51
  • 8
  • Note that reading and writing files nowadays is done by using Javas new I/O API called **NIO**. It revolves around the classes `Files`, `Paths` and `Path`. – Zabuzard Dec 04 '17 at 15:47
  • "However one of the lines has a visible 'gap' or several whitespaces" how should that gap be interpreted? I am assuming you are referring to `mp1 6 5 6 " " 7 5` but there that gap contains `"`, are those quotation mark also part of real file/input? Should we assume that this `" "` represents some kind of element "space" element surrounded by spaces which should be included in result array, or maybe those spaces should be treated as one delimiter? – Pshemo Dec 04 '17 at 15:50
  • Potential duplicate (depending on OP clarification) [Splitting a string with multiple spaces](https://stackoverflow.com/q/10079415) – Pshemo Dec 04 '17 at 16:13

1 Answers1

1
String tokens[]=line.split(" ");

This only seperates on a single white space. Change it to

String tokens[]=line.split("\\s+");

This is any amount of whitespace

This is a useful guideline for different regex pattern composites.

\\s - matches single whitespace character

\\s+ - matches sequence of one or more whitespace characters.