Edit: figured it out, misplaced a piece of code
I have a program that calculates slopes from a file. The format the file is in is like this:
Y2 ‘space’ Y1 ‘space’ X2 ‘space’ X1 ‘space’
I am using a Scanner to read Strings from the file then converting them to a double or integer. The reason I am doing this is because it seems like it doesn’t read doubles or integers from a text file only strings. Here is some of my code:
modelSlopes.clear();
modelValues.clear();
int returnVal = openFileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = openFileChooser.getSelectedFile();
try {
Scanner fileScanner = new Scanner(new FileReader(file));
int count = 1;
boolean suc = true;
while (fileScanner.hasNext()) {
suc = true;
double tmp1 = 0;
double tmp2 = 0;
double tmp3 = 0;
double tmp4 = 0;
try {
if(count == 1) {
tmp1 = Double.valueOf(fileScanner.next());
}
if(count == 2) {
tmp2 = Double.valueOf(fileScanner.next());
}
if(count == 3) {
tmp3 = Double.valueOf(fileScanner.next());
}
if(count == 4) {
tmp4 = Double.valueOf(fileScanner.next());
}
} catch (NumberFormatException e) {
try {
if(count == 1) {
tmp1 = Integer.valueOf(fileScanner.next());
}
if(count == 2) {
tmp2 = Integer.valueOf(fileScanner.next());
}
if(count == 3) {
tmp3 = Integer.valueOf(fileScanner.next());
}
if(count == 4) {
tmp4 = Integer.valueOf(fileScanner.next());
}
} catch(NumberFormatException e1) {
suc = false;
}
}
if(suc) {
if(count != 4) {
count++;
}
if(count == 4) {
count = 1;
SlopeSolver tmpS = new SlopeSolver(Double.valueOf(tmp1), Double.valueOf(tmp2), Double.valueOf(tmp3), Double.valueOf(tmp4));
modelSlopes.addElement(tmpS.getSlope());
modelValues.addElement("Y2 - " + String.valueOf(tmp1) + "; Y1 - " + String.valueOf(tmp2) + "; X2 - " + String.valueOf(tmp3) + "; X1 - " + String.valueOf(tmp4));
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error opening or with file");
}
} else {
}
When I do this it displays 0’s for the slope and the values. I can’t seem to get it to work. I am new to java, and I am pretty clueless right now. Any help would be appreciated.
Here is the text file I am reading data from:
0.0 2.0 0.0 1.0
~Andrew
I would simplify the code