2

私のデータファイルは次のようになります(1列目-x; 2列目-y; 3列目):

    46    80     2
    95    75     2
    78    85     2
    59    54     1
    81    52     2
    57    78     1
    72    46     2

タイプに応じて、ポイントの2つの異なる配列リストにx座標とy座標を保存しようとしています。

import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;

public class program {

    public static void main(String []args)  {
        ArrayList knots = new ArrayList<Point>();
        ArrayList zeros = new ArrayList<Point>();
        List<Integer> list = new ArrayList<Integer>();
        String line = null;
        String file = "hepatitis_data1.txt";
        BufferedReader reader;
        try  {
            reader = new BufferedReader(new FileReader(file));
            while((line = reader.readLine()) != null)  {
                String tmp[] = line.split(" +");

                System.out.println(line);
                for (String s:tmp) {
                    s = s.trim();
                    if(s.length() > 0)  {
                    int i = Integer.parseInt(s.trim());
                    int r = Integer.parseInt(tmp[1].trim());
                    int g = Integer.parseInt(tmp[2].trim());
                    if (tmp[3].equals("1"))  {
                        knots.add(new Point(r,g));
                    }
                    else if(tmp[3].equals("2"))  {
                        zeros.add(new Point(r,g));
                    }
                }
            }
        }

        catch (FileNotFoundException e)  {
            e.printStackTrace();
        } catch (IOException e)  {
            e.printStackTrace();
        }

        int total = knots.size() + zeros.size();
        System.out.println("knot size" + knots.size());
        System.out.println("zero size" + zeros.size());
        System.out.println("total size" + total);

    }   
}

エラーは表示されませんが、正しいことをしていません。xy座標のペアが83あるため、合計値は83になるはずですが、合計は240になります。

4

3 に答える 3

1

ファイルを読み取るステートメントを実行whileしており、その中で各行に対してforループを実行しています。各行で for ループが 3 回発生しています (!!!!) まさにそこに問題があります。行う:

import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;

public class program {

    public static void main(String []args)  {
        ArrayList knots = new ArrayList<Point>();
        ArrayList zeros = new ArrayList<Point>();
        List<Integer> list = new ArrayList<Integer>();
        String line = null;
        String file = "hepatitis_data1.txt";
        BufferedReader reader;
        try  {
            reader = new BufferedReader(new FileReader(file));
            while((line = reader.readLine()) != null)  {
                String tmp[] = line.split(" +");

                System.out.println(line);
                String s = tmp[0];
                    s = s.trim();
                    if(s.length() > 0)  {
                    int i = Integer.parseInt(s.trim());
                    int r = Integer.parseInt(tmp[1].trim());
                    int g = Integer.parseInt(tmp[2].trim());
                    if (tmp[3].equals("1"))  {
                        knots.add(new Point(r,g));
                    }
                    else if(tmp[3].equals("2"))  {
                        zeros.add(new Point(r,g));
                    }
            }
        }

        catch (FileNotFoundException e)  {
            e.printStackTrace();
        } catch (IOException e)  {
            e.printStackTrace();
        }

        int total = knots.size() + zeros.size();
        System.out.println("knot size" + knots.size());
        System.out.println("zero size" + zeros.size());
        System.out.println("total size" + total);

    }   
}
于 2013-09-03T05:34:20.867 に答える
0

前に述べたように、forあなたが使用していた強化されたループに関係していたように、あなたが提供したサンプルデータで動作するようになりましたArrayLists. 私のリビジョン:

import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;

public class program {

public static void main(String []args)  {
    ArrayList knots = new ArrayList<Point>();
    ArrayList zeros = new ArrayList<Point>();
    List<Integer> list = new ArrayList<Integer>();
    String line = null;
    String file = "hepatitis_data1.txt";
    BufferedReader reader;
    try  {
        reader = new BufferedReader(new FileReader(file));
        while((line = reader.readLine()) != null)  {
            String tmp[] = line.split(" +");

            System.out.println(line);
            int x = Integer.parseInt(tmp[1].trim());
            int y = Integer.parseInt(tmp[2].trim());
            int type = Integer.parseInt(tmp[3].trim());
            if(type == 1)
                knots.add(new Point(x,y));
            if(type == 2)
                zeros.add(new Point(x,y));
        }

    }

    catch (FileNotFoundException e)  {
        e.printStackTrace();
    } catch (IOException e)  {
        e.printStackTrace();
    }
    int total = knots.size() + zeros.size();
    System.out.println("knot size " + knots.size());
    System.out.println("zero size " + zeros.size());
    System.out.println("total size " + total);
        }

    }

私があなたに提案できるのは、この特定Scannerのケースではなく、あなたへの提案です。を解析せずに直接読み取るために使用できるメソッドがありますBufferedReaderScannernextInt()intsStrings

于 2013-09-03T06:30:47.053 に答える
0

以下のコードは、ノットとゼロのいずれかにデータを追加する行ごとに 3 回 (3 回) 繰り返されると思います。したがって、合計値は 240 になります。

for (String s:tmp) {
                s = s.trim();
                if(s.length() > 0)  {
                int i = Integer.parseInt(s.trim());
                int r = Integer.parseInt(tmp[1].trim());
                int g = Integer.parseInt(tmp[2].trim());
                if (tmp[3].equals("1"))  {
                    knots.add(new Point(r,g));
                }
                else if(tmp[3].equals("2"))  {
                    zeros.add(new Point(r,g));
                }


          }

それは役立つかもしれません

ありがとう

于 2013-09-03T05:31:19.100 に答える