【问题标题】:Adding numbers to arraylist from text file java从文本文件java将数字添加到arraylist
【发布时间】:2013-09-03 05:18:56
【问题描述】:

我的数据文件如下所示(第 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

我正在尝试将 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);

    }   
}

它没有显示任何错误,但它也没有做正确的事情。总值应为 83,因为我有 83 对 x-y 坐标,但总数为 240。 有什么帮助吗??

【问题讨论】:

  • 你确定吗?您是否调试并查看了列表中的内容?
  • 是的,我尝试打印所有内容,但找不到错误。列表中没有任何内容,我忘记删除了。
  • if (tmp[3].equals("1") 中是tmp[3]还是tmp[2]?

标签: java text arraylist coordinates point


【解决方案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);

    }   
}

【讨论】:

  • 只需将 tmp[0].length 更改为 tmp[1].length 即可。但不明白为什么会这样。
  • @user2398101 我已经编辑了答案,不明白前一个问题出了什么问题......这会起作用
  • @user2398101 我不确定我是否理解,但如果我的回答对您有帮助,请接受它
【解决方案2】:

我认为下面的代码每行重复三次,它将数据添加到节点和零中(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));
                }


          }

可能会有帮助

谢谢

【讨论】:

    【解决方案3】:

    我让它与您提供的示例数据一起工作,正如之前所述,它与您使用的增强型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 而不是BufferedReader。 Scanner 具有nextInt() 方法,您可以使用该方法直接读取ints,而无需解析任何Strings

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2018-10-29
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多