简体   繁体   English

在Java中无法从扫描仪读取双打

[英]Reading Doubles from a Scanner in Java not working out

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' Y2'空间'Y1'空间'X2'空间'X1'空间'

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. 当我这样做时,它的斜率和值显示为0。 I can't seem to get it to work. 我似乎无法正常工作。 I am new to java, and I am pretty clueless right now. 我是Java的新手,现在很笨。 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 我会简化代码

Scanner sc = new Scanner(new FileReader(file));
SlopeSolver ss = new SlopeSolver(sc.nextDouble(),
                              sc.nextDouble(), sc.nextDouble(), sc.nextDouble());
sc.close();

使用fileScanner.nextDouble()读取double形式的用户输入,并且fileScanner.nextInt()读取int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM