hi.
I read a .txt file and i want to put the values from it , to an array. I have dificulties.
here is the code i am using:
public void read(File afile) {
FileInputStream fInpStr = null;
BufferedReader bRead = null;
StringTokenizer strTok = null;
String line = null;
String next = null;
//temporary variables
String variableName;
double variableValue;
//open the variable text file and catch any error.
try {
fInpStr = new FileInputStream(afile);
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "File not found " + afile,
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
variableName="";
variableValue=0.0;
try {
// Read in a line at a time from the text file
bRead = new BufferedReader(new InputStreamReader(fInpStr));
// first line
variableName = bRead.readLine();
// second line
line = bRead.readLine();
while (line != null) {
strTok = new StringTokenizer(line,"/n",true);
try {
double []jim = new double[5];
next = strTok.nextToken();
for (int i = 0; i < 3; i++) {
variableValue = Double.parseDouble(next);
jim [i]= variableValue;
}
System.out.println(jim[1]);
}
catch (NumberFormatException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Error reading file "
+ afile, "Error",
JOptionPane.ERROR_MESSAGE);
}
line = bRead.readLine();
}
fInpStr.close();
}
catch (IOException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Error reading file " + afile,
"Error", JOptionPane.ERROR_MESSAGE);
}
} //end of read
}//end of class
The problem is that by this code insted of printing only one value (jim[1]), it prints all the values i have passed in from the .txt file.
Do i miss anything??
I cannot understand.
Thanks.