Reputation: 707
I'm developing a Java application that make some statistic stuff.
This application take all data from a .txt file which is supplied by the user. The first line of that file contains the name of the sets of data that follows like this:
velx,vely,velz
//various data
I need to analyze that first line and retrieve the three name of variables, I correctly get the first two but I'm not able to get the last one.
There the code to get names:
public ArrayList<String> getTitle(){
// the ArrayList originally is not here but in the class intestation
// I copied it here to simplify code's understanding
ArrayList<String> title = new ArrayList<String>();
try {
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
int titleN = 0;
String line = br.readLine(); //read the first line of file
String temp;
System.out.println(ManageTable.class.getName() + " Line: " + line);
int c = line.length();
for(int i = 0; i <c; i++){
if((line.charAt(i) == ',') || **ANOTHER CONDITION** ){
temp = sb.toString();
System.out.println(ManageTable.class.getName() +" Temp is: " + temp);
title.add(temp);
System.out.println(ManageTable.class.getName() + " Title added");
sb.delete(0, sb.length());
}else{
sb.append(line.charAt(i));
}
}
} catch (IOException ex) {
Logger.getLogger(ManageTable.class.getName()).log(Level.SEVERE, null, ex);
}
return title;
}
I need to add a second condition to the if statement in order to find out when the line is ended and save the last name, even if its not followed by ',' I tried using:
if((line.charAt(i) == ',') || (i==c))
but from the name I get, always miss a character. How can I check the end of the line and so get the full name?
Upvotes: 0
Views: 117
Reputation: 72844
No need for all this looping. You can just split the line around the comma to get an array:
String[] names = line.split(",");
Upvotes: 1
Reputation: 11609
If line contains just three names separated by comma, you can do
String[] names = line.split(",");
Upvotes: 4