MTT
MTT

Reputation: 5263

Reading data from a text file in Java

I have a problem in reading data from a text file and put it in 2 dimensional array. The sample of dataset is:

1,2,3,4,5,6

1.2,2.3,4.5,5.67,7.43,8

The problem of this code is that it just read the first line and does not read the next lines. Any suggestion is appreciated.

package test1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test1{ 

public static void main(String args[])throws FileNotFoundException, IOException{
try{    
   
double[][] X = new double[2][6];
BufferedReader input = new BufferedReader(new FileReader(file));

String [] temp;
String line = input.readLine();
String delims = ",";
temp = line.split(delims);
int rowCounter = 0;
while ((line = input.readLine())!= null) {
for(int i = 0; i<6; i++){
X[rowCounter][i] = Double.parseDouble(temp[i]);
}
    
rowCounter++;
} 

}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
}finally{
}
}
}

Upvotes: 0

Views: 1540

Answers (5)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I couldn't run the code, but one of your problems is that you were only splitting the first text line.

package Test1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test1 {

    public static void main(String args[]) {
        try {
            double[][] X = new double[2][];
            BufferedReader input = new BufferedReader(new FileReader(file));

            String line = null;
            String delims = ",";

            int rowCounter = 0;
            while ((line = input.readLine()) != null) {
                String[] temp = line.split(delims);
                for (int i = 0; i < temp.length; i++) {
                    X[rowCounter][i] = Double.parseDouble(temp[i]);
                }
                rowCounter++;
            }

        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        } finally {
        }
    }
}

I formatted your code to make it more readable.

I deferred setting the size of the second element of the two dimensional array until I knew how many numbers were on a line.

Upvotes: 0

serkan
serkan

Reputation: 603

readLine expects a new line character at the end of the line. You should put a blank line to read the last line or use read instead.

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Try:

int rowCounter = 0;
while ((line = input.readLine())!= null) {
String [] temp;
String line = input.readLine();
String delims = ",";
temp = line.split(delims);
for(int i = 0; i<6; i++){
X[rowCounter][i] = Double.parseDouble(temp[i]);
}
...

Upvotes: 1

rgettman
rgettman

Reputation: 178253

The only place that your temp array is being assigned is before your while loop. You need to assign your temp array inside the loop, and don't read from the BufferedReader until the loop.

String[] temp;
String line;
String delims = ",";
int rowCounter = 0;
while ((line = input.readLine())!= null) {
    temp = line.split(delims);  // Moved inside the loop.
    for(int i = 0; i<6; i++){
    X[rowCounter][i] = Double.parseDouble(temp[i]);
}

Upvotes: 1

Farlan
Farlan

Reputation: 1900

Have you tried the Array utilities? Something like this:

while ((line = input.readLine())!= null) {  
  List<String> someList = Arrays.asList(line.split(","));
  //do your conversion to double here
  rowCounter++;
}

I think the blank line might be throwing your for loop off

Upvotes: 2

Related Questions