Java 为什么我的for对象循环没有打印出来?

Java 为什么我的for对象循环没有打印出来?,java,arrays,object,arraylist,Java,Arrays,Object,Arraylist,我觉得至少打印出每个子类的toString方法有点正确,但由于某种原因,它导致没有打印任何内容。任何帮助都将不胜感激 data.txt: 1 meter 1 inch 1 foot 1 yard 401.336 meters 15839 inches 1319 feet 439 yards 代码 公共抽象类的长度{ 公共字符串toString(){ 返回这个.getClass()+“:“+getLength()+”“+getUnit(); } 公共静态void main(字符串[]args){

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

data.txt:

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

公共抽象类的长度{
公共字符串toString(){
返回这个.getClass()+“:“+getLength()+”“+getUnit();
}
公共静态void main(字符串[]args){
ArrayList lo=新的ArrayList();
扫描仪输入=空;
试一试{
in=新扫描仪(新文件(“src/length/data.txt”);
}捕获(FileNotFoundException异常){
抛出新的RuntimeException(“无法打开data.txt”);
}
//此作业的其他部分需要更多代码吗
while(在.hasNextDouble()中){
double value=in.nextDouble();
字符串单位=in.next();
长度=空;
如果(单位等于(“米”)){
长度=新仪表(数值);
加上(长度);
}
否则,如果(单位等于(“英尺”)){
长度=新脚(值);
加上(长度);
}
否则,如果(单位等于(“英寸”)){
长度=新英寸(值);
加上(长度);
}
否则,如果(单位等于(“码”)){
长度=新码(值);
加上(长度);
}
}
Length[]myLengths=新长度[lo.size()];//声明myLengths整数数组
对于(inti=0;i
此代码中的问题是字符串比较。请使用unit.equals(“英寸”)等代替unit.equalsIgnoreCase(“英寸”)等。更正代码:

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());
        }
    }
}
公共抽象类的长度{
公共字符串toString(){
返回这个.getClass()+“:“+getLength()+”“+getUnit();
}
公共静态void main(字符串[]args){
ArrayList lo=新的ArrayList();
扫描仪输入=空;
试一试{
in=新扫描仪(新文件(“src/length/data.txt”);
}捕获(FileNotFoundException异常){
抛出新的RuntimeException(“无法打开data.txt”);
}
//此作业的其他部分需要更多代码吗
while(在.hasNextDouble()中){
double value=in.nextDouble();
字符串单位=in.next();
长度=空;
if(单位:等信号量(“米”)){
长度=新仪表(数值);
加上(长度);
}else if(单位为等效信号(“英尺”)){
长度=新脚(值);
加上(长度);
}else if(单位:等信号情况(“英寸”)){
长度=新英寸(值);
加上(长度);
}否则,如果(单位为同等信号(“码”)){
长度=新码(值);
加上(长度);
}
}
Length[]myLengths=新长度[lo.size()];//声明myLengths整数数组
对于(int i=0;i
在单位字符串比较中,代码中有两个问题

  • 其他答案提到,在代码中使用“Inch”,而在文件中使用“Inch”。可以使用
    equalsIgnoreCase()

  • 即使您更改了1,您仍将面临读取不完整文件的问题。原因是在文件中,您将遇到单数和复数单位名称。可以通过执行类似于
    if(unit.equalsIgnoreCase(“foot”)| unit.equalsIgnoreCase(“foots”)


  • 你的文件包含什么?看起来像
    MyLength.length
    是0。这是data.txt的内容1米1英寸1英尺1码401.336米15839英寸1319英尺439码不要在这里发布。编辑您的问题并将其添加到那里,格式化。您的
    toString()
    方法在类
    Length
    中是什么样子的?
    myLengths=lo.toArray()
    无需for循环来创建数组
    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());
            }
        }
    }