This might not be the cause, but do not split by space. You may have tabs or LF characters, and/or leading and trailing spaces.
That is, do not use lines[x].split(" ");
split using regex.
lines[x].split("[\\s]+"); //one or more spaces.
Question
I have a requirement where in I need to read contents of a file, each line needs to be read into an array and split the line and store the values into separate variables for further processing.
Sample data from the file
175 31 4 877108051
62 827 2 879373421
138 100 5 879022956
252 9 5 891456797
59 421 5 888206015
Expected output
arr[1][1] = 175
arr[1][2] = 31
arr[1][3] = 2
arr[1][4] = 879373421
arr[2][1] = 62
arr[2][2] = 827
arr[2][3] = 4
arr[2][4] = 877108051
.
.
.
Im able to split properly using StringTokenizer but unable to access individual elements after tokens are formed. Tried with Array.split(), but its not splitting the values into separate array values. Please help me out. Thanks in advance.
My code
static public void main(String[] args) {
File file = new File("small.txt");
String[] lines = new String[100];
FileReader reader = new FileReader(file);
BufferedReader buffReader = new BufferedReader(reader);
int x = 0;
String s;
while((s = buffReader.readLine()) != null){
lines[x] = s;
// Using StringTokenizer
StringTokenizer st = new StringTokenizer(lines[x]);
while (st.hasMoreTokens()){
System.out.println("Next token:"+st.nextToken());
}
// Using Split()
String[] split1 = lines[x].split(" ");
int size = split1.length;
System.out.println("Split size:"+size);
int i = 0;
for (String value : split1) {
System.out.println("Index:"+i+" "+ value); i++;}
}
Solution 2
This might not be the cause, but do not split by space. You may have tabs or LF characters, and/or leading and trailing spaces.
That is, do not use lines[x].split(" ");
split using regex.
lines[x].split("[\\s]+"); //one or more spaces.
OTHER TIPS
You can simplify this code a lot. Try something like this.
1) Read the file line by line, split lines as you go,
add values to some ArrayList
containing String[]
2) Close your file
3) Turn the ArrayList
into a String[][]
4) Print the result
Also, note that arrays in Java are indexed starting at 0 not at 1.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
public class Test021 {
static public void main(String[] args) throws Exception {
File file = new File("small.txt");
FileReader reader = new FileReader(file);
BufferedReader buffReader = new BufferedReader(reader);
String s = null;
ArrayList<String[]> lst = new ArrayList<String[]>();
String[][] res = null;
while((s = buffReader.readLine()) != null){
String[] arr = s.split("[\\s]+");
lst.add(arr);
}
buffReader.close();
res = new String[lst.size()][lst.get(0).length];
res = lst.toArray(res);
System.out.println();
// System.out.println(res);
// String result = Arrays.deepToString(res);
// System.out.println(result);
System.out.println();
for (int i=0; i<res.length; i++){
for (int j=0; j<res[i].length; j++){
System.out.println("res[" + (i+1) + "][" + (j+1) + "]=" + res[i][j]);
}
}
System.out.println();
}
}
OUTPUT:
res[1][1]=175
res[1][2]=31
res[1][3]=4
res[1][4]=877108051
res[2][1]=62
res[2][2]=827
res[2][3]=2
res[2][4]=879373421
res[3][1]=138
res[3][2]=100
res[3][3]=5
res[3][4]=879022956
res[4][1]=252
res[4][2]=9
res[4][3]=5
res[4][4]=891456797
res[5][1]=59
res[5][2]=421
res[5][3]=5
res[5][4]=888206015
Try this code
List<String> lines=new ArrayList<>();
Read file and add line to lines array
File file = new File("data.txt");
FileReader reader = new FileReader(file);
BufferedReader buffReader = new BufferedReader(reader);
String line = buffReader.readLine();
while(line!=null){
lines.add(line);
line = buffReader.readLine();
}
Convert lines array to string 2d array
String string[][]=new String[lines.size()][0];
for(int i=0;i<lines.size();i++){
String s[]=lines.get(i).split(" ");
string[i]=s;
}