在 Java 中从扫描仪读取双精度数不起作用

发布于 2024-12-17 15:54:24 字数 3039 浏览 0 评论 0原文

编辑:弄清楚了,放错了一段代码

我有一个从文件计算斜率的程序。该文件的格式如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-12-24 15:54:24

我会简化代码

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

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();
美煞众生 2024-12-24 15:54:24

使用 fileScanner.nextDouble() 从用户输入中读取 double,使用 fileScanner.nextInt() 读取 int >。

Use fileScanner.nextDouble() to read double form the user input, and fileScanner.nextInt() to read int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文