在 Java 中从扫描仪读取双精度数不起作用
编辑:弄清楚了,放错了一段代码
我有一个从文件计算斜率的程序。该文件的格式如下:
Y2“空间”Y1“空间”X2“空间”X1“空间”
我正在使用扫描仪从文件中读取字符串,然后将它们转换为双精度或整数。我这样做的原因是因为它似乎不从文本文件中仅读取字符串中的双精度数或整数。这是我的一些代码:
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 {
}
当我这样做时,它会显示斜率和值的 0。我似乎无法让它发挥作用。我是java新手,现在一无所知。任何帮助将不胜感激。
这是我正在读取数据的文本文件:
0.0 2.0 0.0 1.0
〜Andrew
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会简化代码
I would simplify the code
使用
fileScanner.nextDouble()
从用户输入中读取double
,使用fileScanner.nextInt()
读取int
>。Use
fileScanner.nextDouble()
to readdouble
form the user input, andfileScanner.nextInt()
to readint
.