【问题标题】:Why isnt my for loop of objects printing out?为什么我的对象循环没有打印出来?
【发布时间】:2014-02-18 04:41:37
【问题描述】:

我觉得至少打印出每个子类的 toString 方法是正确的,但由于某种原因,它导致什么都没有打印出来。任何帮助将不胜感激。

数据.txt:

1 meter
1 inch
1 foot
1 yard
401.336 meters
15839 inches
1319 feet
439 yards

代码

public abstract class Length implements Comparable<Length> {

    public String toString() {
        return this.getClass() + ": " + getLength() + " " + getUnit();
    }



public static void main(String[] args){
        ArrayList<Length> lo = new ArrayList <Length>();
        Scanner in = null;
        try {
            in = new Scanner(new File("src/length/data.txt"));
        } catch (FileNotFoundException exception) {
            throw new RuntimeException("failed to open data.txt");
        }
        // need more code for other parts of this assignment
        while (in.hasNextDouble()) {
            double value = in.nextDouble();
            String unit = in.next();
            Length length = null;

            if(unit.equals("Meter")){
                length = new Meter(value);
                lo.add(length);             
            }

            else if(unit.equals("Foot")){

                length = new Foot(value);
                lo.add(length);
            }

            else if(unit.equals("Inch")){
                length= new Inch(value);
                lo.add(length);
            }

            else if(unit.equals("Yard")){
                length = new Yard(value);
                lo.add(length);
            }

        }
        Length[] myLengths = new Length[lo.size()]; //declares myLengths integer array 
        for(int i=0; i < lo.size(); i++){       
            myLengths[i]=lo.get(i);     //copies arrayList to lo to Length array myLengths

        }


    for(int i = 0; i < myLengths.length; i++) {
            System.out.println(myLengths[i].toString());  
        }
    }   
}

【问题讨论】:

  • 您的文件包含什么?好像myLengths.length 是 0。
  • 这是data.txt的内容 1米1英寸1英尺1码401.336米15839英寸1319英尺439码
  • 请勿在此处发布。编辑您的问题并将其添加到那里,格式化。
  • 您的toString() 方法在Length 类中是什么样的?
  • myLengths = lo.toArray() 无需 for 循环来创建数组

标签: java arrays object arraylist


【解决方案1】:

这段代码的问题是字符串比较。请使用 unit.equalsIgnoreCase("Inch") 等代替 unit.equals("Inch") 更正代码:

public abstract class Length implements Comparable<Length> {

    public String toString() {
        return this.getClass() + ": " + getLength() + " " + getUnit();
    }

    public static void main(String[] args) {
        ArrayList<Length> lo = new ArrayList<Length>();
        Scanner in = null;
        try {
            in = new Scanner(new File("src/length/data.txt"));
        } catch (FileNotFoundException exception) {
            throw new RuntimeException("failed to open data.txt");
        }
        // need more code for other parts of this assignment
        while (in.hasNextDouble()) {
            double value = in.nextDouble();
            String unit = in.next();
            Length length = null;

            if (unit.equalsIgnoreCase("Meter")) {
                length = new Meter(value);
                lo.add(length);
            } else if (unit.equalsIgnoreCase("Foot")) {

                length = new Foot(value);
                lo.add(length);
            } else if (unit.equalsIgnoreCase("Inch")) {
                length = new Inch(value);
                lo.add(length);
            } else if (unit.equalsIgnoreCase("Yard")) {
                length = new Yard(value);
                lo.add(length);
            }

        }
        Length[] myLengths = new Length[lo.size()]; //declares myLengths integer array 
        for (int i = 0; i < lo.size(); i++) {
            myLengths[i] = lo.get(i);     //copies arrayList to lo to Length array myLengths

        }


        for (int i = 0; i < myLengths.length; i++) {
            System.out.println(myLengths[i].toString());
        }
    }
}

【讨论】:

    【解决方案2】:

    你的代码在单位字符串比较中有两个问题。

    1. 其他答案已经提到,在代码中您使用“英寸”,而在文件中您使用“英寸”。可以使用equalsIgnoreCase()

    2. 解决
    3. 即使你做了1的改变,你仍然会面临读取不完整文件的问题。原因是在文件中,您会遇到单数和复数的单位名称。可以通过if (unit.equalsIgnoreCase("foot") || unit.equalsIgnoreCase("feet"))

    4. 之类的操作来解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多