简体   繁体   中英

While loop trouble for a file reader?

I am creating a program for class that reads in a CSV file and does various mathematical equations on the data given in the file. The CSV file is in the set up:

  • longitude1, latitude1, timeStamp1
  • longitude2, latitude2, timeStamp2
  • longitude3, latitude3, timeStamp3

(and it continues on for approximately 100 different coordinates).

Right now I am focusing on the longitude values---I am having difficulty getting my while loop to perform the operations i need it too. I need to be able to get the program to subtract longitude2 from longitude1 (where lon1 and lon2 are changing to as we go through the loop...ultimately going through every coordinate).

The problem I am having is that the loop isn't creating an 'overlap' (for lack of better term). It's performing the math on the first two coordinates and then skipping down to the next two... thus skipping every other operation.

[ ie : the loop performing: lon2--lon1 and lon4-lon3 but is skipping lon3-lon2 ]

I have a hunch that my problem lies at the start of my while loop but I honestly don't know how to fix it. I would love some help. Below is the code I have currently:

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

Its because you're reading 2 lines in one loop run. I'd suggest do something like this:

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;
}

Why its not working as you intended? Well, imagine a pointer thats currently at the beggining of your file. With fr.nextLine() you are reading line its currently pointed and the pointer goes down. In one run of your loop you read 2 consecutive lines, do your math and in next run again reading 2 more lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM