2012-03-24 7 views
1

私は、CSVファイルを読み込み、そのファイルに与えられたデータについてさまざまな数学的方程式を実行するクラス用のプログラムを作成しています。 ファイルリーダーのループの問題?

(と、それは約のために続けて、latitude1

  • longitude1、timeStamp3
  • timeStamp1 latitude2
  • longitude2、timeStamp2 latitude3
  • longitude3、: CSVファイルを設定しています100の異なる座標)。

    今私は経度の値に焦点を当てています---私はwhileループでも必要な操作を実行するのが難しいです。私はlongitude1からlongitude2を引くプログラムを得る必要があります(lon1とlon2はループを通り抜けて行きます...最終的にすべての座標を通ります)。

    私が抱えている問題は、ループが「オーバーラップ」(より良い用語が不足している)を作成していないことです。最初の2つの座標で数学を実行してから、次の2つにスキップして、他のすべての操作をスキップします。

    [すなわち:ループパフォーマンス:lon2 - lon1とlon4-lon3が、lon3-lon2を飛ばしている]

    私は私の問題は、私のwhileループの開始時にあると直感を持っているが、私は正直ドン」それを修正する方法を知っている。私はいくつかの助けが大好きです。あなたが1つのループの実行中に2行を読んでいるので

    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 
    

答えて

3

その:以下 は、私が現在持っているコードです。私はこのような何かをお勧めします:

​​

なぜあなたは意図したとおりに動作していませんか?さて、あなたのファイルの先頭にポインタがあると想像してください。 fr.nextLine()を使用すると、現在ポイントされている行が読み込まれ、ポインタが下がります。 ループの1回の実行で2つの連続した行を読み、数学を行い、次に2つの行をもう一度読み直します