简体   繁体   中英

Why isnt my for loop of objects printing out?

I feel like this is somewhat right to at least print out the toString method of each subclass but for some reason its resulting in nothing being printed whatsoever. Any help will be greatly appreciated.

data.txt:

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

code

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

The problem in this code is the string comparison. Instead of unit.equals("Inch"), please use unit.equalsIgnoreCase("Inch") etc. Corrected code:

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

there are two problems in your code in the unit string comparison.

  1. other answers have mentioned, in code you are using "Inch" while in file you are using "inch". It can be solved by using equalsIgnoreCase()

  2. Even you made the change of 1, you will still face problem of reading incomplete file. The reason is in the file, you will encounter both singular and plural unit name. It can be solved by doing somehting like if (unit.equalsIgnoreCase("foot") || unit.equalsIgnoreCase("feet"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM