Java 程序似乎每隔一行读取一次.txt文件

Java 程序似乎每隔一行读取一次.txt文件,java,file-io,Java,File Io,我正在做一个作业,在作业中我使用扫描器读取.txt文件的行和标记。我需要进行一些转换并重新排列一些字符串,这是我使用一些助手方法完成的。问题是代码只对从文件中读取的每一行起作用。我想我可能把代码开头的某个地方搞砸了。关于我需要修改的内容有什么提示或提示吗?这是我得到的,苏斯法: public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File

我正在做一个作业,在作业中我使用扫描器读取.txt文件的行和标记。我需要进行一些转换并重新排列一些字符串,这是我使用一些助手方法完成的。问题是代码只对从文件中读取的每一行起作用。我想我可能把代码开头的某个地方搞砸了。关于我需要修改的内容有什么提示或提示吗?这是我得到的,苏斯法:

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("PortlandWeather2013.txt"));
    while (input.hasNextLine()) {
        String header = input.nextLine();
        System.out.println(header);

        Scanner input2 = new Scanner(header);

        while (input2.hasNextLine()){
            String bottomHeader = input.nextLine();
            System.out.println(bottomHeader);

            String dataLines = input.nextLine();

            Scanner linescan = new Scanner(dataLines);

            while (linescan.hasNext()){

                String station = linescan.next();
                System.out.print(station+ " ");

                String wrongdate = linescan.next();
                String year = wrongdate.substring(0,4) ;
                String day = wrongdate.substring(6);
                String month = wrongdate.substring(4,6);

                System.out.print(month + "/" + day + "/" + year);

                double prcp = linescan.nextDouble();
                System.out.print("\t  "+prcpConvert(prcp));

                double snwd = linescan.nextDouble();
                System.out.print("\t  " + snowConvert(snwd));

                double snow = linescan.nextDouble();
                System.out.print("\t" + snowConvert(snow));

                double tmax = linescan.nextDouble();
                System.out.print("\t" + tempConvert(tmax));

                double tmin = linescan.nextDouble();
                System.out.println("\t" + tempConvert(tmin));


            }

        }
    }
}

public static double prcpConvert(double x){

    double MM = x/1000;
    double In = MM * 0.039370;
    double rounded = Math.round(In * 10)/10;
    return rounded;


}
public static double snowConvert(double x){
    double In = x * 0.039370;
    double rounded = Math.round(In * 10)/10;

    return rounded;
}

public static double tempConvert(double x){
    double celsius = x/10;
    double fahrenheit = (celsius *9/5)+32;
    double rounded = Math.round(fahrenheit *10)/10;

    return rounded;
}
nextLine不仅获取最后一行,它还向前推进一行。在第二个循环之前,您将调用nextLine两次,从而使您在循环的每次迭代中前进两行


您可以通过设置dataLines=bottomHeader而不是再次调用nextLine来解决问题。

为什么要为简单字符串创建新的Scanner对象?另外,请发布PortlandWeather2013的内容以及应用程序的功能。没有这些信息,除了语法错误之外,我们无法帮助您。