添加数字,从文本文件中的Java的ArrayList [英] Adding numbers to arraylist from text file java

查看:161
本文介绍了添加数字,从文本文件中的Java的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据文件看起来像这样(第1列-X; 2-Y; 3型):

My data file looks like this( 1st column-x; 2nd-y; 3rd-type):

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

我试图根据其类型保存的X和Y坐标点的两个不同的ArrayList

I am trying to save x and y coordinates in two different arraylists of points depending on their type.

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

    }   
}

它不显示任何错误,但它不是做正确的事情无论是。总价值应该是83,因为我有83双x-y坐标,但一共是240。
任何帮助??

It doesn't show any error, but its not doing the right thing either. Total value should be 83 as I have 83 pairs of x-y coordinates, but total comes 240. Any help??

推荐答案

你正在做一个,而语句读取该文件,并在其中,每一个你行一个循环。每行的循环正在发生3次(!!!!)这正是问题就在这里。这样做:

you are doing a while statement to read the file, and in it, for each line you do a for loop. for each line the for loop is happening 3 times(!!!!) that's exactly the problem right there. do:

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

    }   
}

这篇关于添加数字,从文本文件中的Java的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆