【问题标题】:i try to read integer values form txt file using eclipes but its give me some error java.lang.NumberFormatException我尝试使用 eclipes 从 txt 文件中读取整数值,但它给了我一些错误 java.lang.NumberFormatException
【发布时间】:2017-03-29 17:05:15
【问题描述】:

我的代码基于 gui,我尝试在 gui 上运行,但没有成功。 公共类 SecondFrame 扩展 JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SecondFrame frame = new SecondFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public SecondFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 612, 469);
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLUE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Success !");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 38));
    lblNewLabel.setBounds(201, 120, 233, 74);
    contentPane.add(lblNewLabel);

    JButton btnGenerateGraph = new JButton("GENERATE GRAPH");
    btnGenerateGraph.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) { 
            try {
                FileReader reader = new FileReader("rn.txt");
                BufferedReader bufferedReader = new BufferedReader(reader);

                String line;
                **here i try to convert line that i has read to integer**
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(Integer.parseInt(line));
                }
                reader.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
    btnGenerateGraph.setFont(new Font("Tahoma", Font.BOLD, 21));
    btnGenerateGraph.setBounds(168, 221, 245, 59);
    contentPane.add(btnGenerateGraph);
}

}

这是错误: 线程“AWT-EventQueue-0”中的异常 java.lang.NumberFormatException:对于输入字符串:“/”

我的 rn.txt 文件:

29 44 15 17

【问题讨论】:

  • 文件 RN.txt 中有一行包含字符“/”,您正试图将其解析为数字。做不到。
  • 29 44 15 17 我的文件看起来像这样 .. 没有字符“/”
  • 在你的 System.out.println(Integer.parseInt(line));行,插入一个 System.out.println(line);这将向您显示该行包含的内容。

标签: java user-interface


【解决方案1】:

读取该行后尝试替换所有新行字符或回车等,然后执行 Integer.parse。

text = text.replace("\n", "").replace("\r", "");

【讨论】:

    【解决方案2】:

    您的文件中似乎有换行符,因此在将其转换为整数时会出错,因为它无法将特殊字符转换为字符串。

    真正找出正在发生的事情的一种方法是首先打印行内容并检查它是否在行尾包含空白或特殊字符。 因此,您将能够确定它在什么时候给出错误。

    String line;
    **here i try to convert line that i has read to integer**
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line); //Print Content as it is to check at which character you are getting error.
        System.out.println(Integer.parseInt(line));
    }
    

    同样,您可以通过在 IDE 中使用调试器来找到它。

    如果您实际上在该行中包含特殊字符或换行符,那么您可以使用下面的代码来删除这些字符。

    String line;
    **here i try to convert line that i has read to integer**
    while ((line = bufferedReader.readLine()) != null) {
        line = replaceAll("(\n)+", "");
        line = replaceAll("(\r)+", "");
        if(line != "" && line!= null){
            System.out.println(line); //Print Content as it is to check at which character you are getting error.
            System.out.println(Integer.parseInt(line));
        }
    }
    

    【讨论】:

    • 感谢 4 你的解释。是的,我的文件实际上数字在垂直线上。当我将输入文件粘贴到显示水平线时。我试试你给定的代码。但它告诉我。 java.io.IOException: 流关闭
    • 您能否将代码粘贴到您最近尝试过的评论中?看起来阅读器在阅读行或解析行内容之前已关闭。带有完整的错误消息
    • line = replaceAll("(\n)+", ""); line = replaceAll("(\r)+", "");
    • 在我输入您建议的 2 行代码后出现此错误:java.io.IOException: Stream closed at sun.nio.cs.StreamDecoder.ensureOpen(Unknown Source) at sun.nio.cs。 StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader。 readLine(Unknown Source) at SecondFrame$2.actionPerformed(SecondFrame.java:67) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    • at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing .plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source)
    【解决方案3】:

    代码没有问题。我正在使用 eclipse 创建 txt 文件。但它没有为我提供 .txt 的扩展名。所以我手动做。复制粘贴到文件夹及其工作。

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 2022-12-21
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      相关资源
      最近更新 更多