【问题标题】:While loop trouble for a file reader?文件阅读器的while循环问题?
【发布时间】:2012-03-24 23:52:40
【问题描述】:

我正在为类创建一个程序,该程序读取 CSV 文件并对文件中给出的数据执行各种数学方程。 CSV 文件在设置中:

  • 经度1,纬度1,时间戳1
  • 经度2、纬度2、时间戳2
  • 经度3、纬度3、时间戳3

(它会持续大约 100 个不同的坐标)。

现在我专注于经度值——我很难让我的 while 循环执行我也需要它的操作。我需要能够让程序从 longitude1 中减去 longitude2 (其中 lon1 和 lon2 在我们通过循环时正在改变......最终通过每个坐标)。

我遇到的问题是循环没有创建“重叠”(因为没有更好的术语)。它在前两个坐标上执行数学运算,然后跳到接下来的两个...因此跳过所有其他操作。

[ 即:循环执行:lon2--lon1 和 lon4-lon3 但正在跳过 lon3-lon2 ]

我有一种预感,我的问题在于我的 while 循环开始,但老实说,我不知道如何解决它。我很想得到一些帮助。 以下是我目前的代码:

import java.io.*;
import java.util.*;
public class SearchAndR {
    public static void main(String[] args) throws FileNotFoundException {
        // creates a Scanner called "keyboard" to read in the file name
        Scanner keyboard = new Scanner(System.in);
        // prompts user for file name
        System.out.println("Enter the name of the file you intend to read in");
        // Stores file name as a string so that we may read it in
        String file = keyboard.nextLine();
        // creates a new Scanner called fr(short for filereader) so we can read
        // in the file
        // then we create a File object using the constructor new with the class
        // "File"
        // this takes in our string "file" as a parameter to read it in
        // reading in our file object to the scanner
        Scanner fr = new Scanner(new File(file));
        // Creating format for hike1.csv file
        System.out.println("--- Hike Analysis ---");
        System.out.println("File: " + file);
        // Calling HW4 Class to use in the rest of the program
        HW4Util util = new HW4Util();
        // Establishing counting variable for distance for the while loop
        double totaldistance = 0;
        while (fr.hasNextLine()) {
            String line = fr.nextLine();
            String[] stamp1 = line.split(",");
            String line2 = fr.nextLine();
            String[] stamp2 = line2.split(",");
            double lon1 = Double.parseDouble(stamp1[0]);
            double lat1 = Double.parseDouble(stamp1[1]);
            String time1 = (stamp1[2]);
            double lon2 = Double.parseDouble(stamp2[0]);
            double lat2 = Double.parseDouble(stamp2[1]);
            String time2 = (stamp2[2]);
            // This is just to check to see if the overlap is occurring!
            // This should not be included in the project. 
            System.out.println("this is longitude" +lon1);
            System.out.println("this is longitude" +lon2);
            // returns the distance between two coordinates as a fraction of a
            // mile
            double dist = util.distance(lat1, lon1, lat2, lon2);
            totaldistance = totaldistance + dist;
            totaldistance = (double) (Math.round(totaldistance * 10000)) / 10000;
        }
        System.out.println("Total distance traveled: " + totaldistance + " miles");
   } // end method main
} // end class filereader

【问题讨论】:

    标签: java while-loop filereader


    【解决方案1】:

    这是因为您在一次循环运行中读取 2 行。我建议做这样的事情:

    String previousLine;
    String[] previousStamp;
    if (fr.hasNextLine())
    {
       previousLine = fr.nextLine();
       previousStamp = previousLine.split(",");
    }
    while (fr.hasNextLine()) {
        String currentLine = fr.nextLine();
        String[] currentStamp = line.split(",");
    
        //do your math here
        //...
    
        //swap values
        previousStamp = currentStamp;
    }
    

    为什么它没有按您的预期工作?好吧,想象一下当前位于文件开头的指针。 使用 fr.nextLine() 您正在读取当前指向的行并且指针向下。 在循环的一次运行中,您连续阅读 2 行,做数学运算,然后在下一次运行中再次阅读 2 行

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 2011-03-04
      • 1970-01-01
      相关资源
      最近更新 更多