Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java语言编号格式异常问题_Java_Io - Fatal编程技术网

Java语言编号格式异常问题

Java语言编号格式异常问题,java,io,Java,Io,我试图将文本文件中的文本作为变量存储在Java程序中 源代码如下: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * Write a description of class Reader here. * * @author (your name) * @version (a version number or a date) */ public

我试图将文本文件中的文本作为变量存储在Java程序中

源代码如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Write a description of class Reader here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Reader
{
    // instance variables - replace the example below with your own
    private int areaNumber;
    private int studyPeriod;
    private int rentalCharges;
    private int CAPEX;
    private int OPEX;
    private double interestRate;

    private int[] rolloutPlanPerArea;
    private int householdsPerArea;
    private double adoptionRatePerArea;

    private String fileName = "practical6a.txt";

    public void run(){
        load(fileName);
    }

    private void load(String fileName){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));

            String currentLine = null;

            while ((currentLine = reader.readLine()) != null) {
                areaNumber = Integer.parseInt(currentLine.trim());
                studyPeriod = Integer.parseInt(currentLine.trim());
                rentalCharges = Integer.parseInt(currentLine.trim());
                CAPEX = Integer.parseInt(currentLine.trim());
                OPEX = Integer.parseInt(currentLine.trim());
                interestRate = Integer.parseInt(currentLine.trim());
            }

            reader.close();

            System.out.print(areaNumber);

        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
    }
    }
每次代码运行时,我都会得到字符串“”的NumberFormatError,就好像程序试图读取的行中没有任何内容一样

文本文件在单独的行中包含值。乙二醇

7
3
2
1
因为每一行上都有一些东西,我质疑为什么没有设置变量

字符串的NumberFormatError
“”


您的文件必须以新的空行结束。要么删除它,要么检查
!调用Integer.parseInt之前的currentLine.trim()等于(“”)

检查字符串是否为空

 if(!currentLine.trim().isEmpty(){
     myInt = Integer.parseInt(currentLine.trim());
 }else{
     myInt = 0;
 }

你得到的是哪一行的错误?你能提供堆栈跟踪吗?你应该把每行得到的打印出来。这将更好地帮助您调试
System.out.println(currentLine)
根据提供的数字,我可以很好地运行它。当我在末尾添加了一个额外的行,或者当我删除了所有的行并点击回车键(因此在末尾有一个LF的行)时,我得到了NumberFormatException。所以我认为拉朱格特是正确的。所以基本上,如果有空行,你需要检查它吗?@Jane是的<在这种情况下,code>readLine将返回空字符串,而不是
null