java - Why isnt my for loop of objects printing out? -


I think printing atString's method of at least every subclass is fine, but due to some reason its anything Do not be anything that is being printed

1 meter 1 inch 1 feet 1 yard 401.336 meters 15839 inches 1319 feet 43 9 yards

Code

  The length of public abstract classes is functional comparative & lt; Length & gt; {Public string toString () {return.getClass ()} + ":" + getLength () + "" + getUnit (); } Public static zero main (string [] args) {ArrayList & lt; Length & gt; Lo = New Arrestist & lt; Length & gt; (); = Null scanner; Try {In = new scanner (New file ("src / length / data.txt")); } Hold (FileNotFoundException exception) {new runtime expiration ("Failed to open data"); } // More parts of this assignment require more code, while (in.hasNextDouble ()) {double value = in.nextDouble (); String unit = in.next (); Length = length = faucet; If (unit angle ("m")) {length = new meter (value); Lo.add (length); } And if (unit angle ("foot")) {length = new foot (value); Lo.add (length); } And if (unit. Angle ("inch")) {length = new inch (value); Lo.add (length); } And if (unit. Angle ("yard")) {length = new yard (value); Lo.add (length); }} Length [] myLengths = new length [lo.size ()]; // myLengths declares an integer array for (int i = 0; i & lt; lo.size (); i ++) {myLengths [i] = lo .get (i); // copy array, myLengths for length array} (int i = 0; i & lt; myLengths.length; i ++) {system.out.println (myLengths [i] .toString ()); }}}}  

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

  Public abstract class length tools comparative & lt; Length & gt; {Public string toString () {return.getClass ()} + ":" + getLength () + "" + getUnit (); } Public static zero main (string [] args) {ArrayList & lt; Length & gt; Lo = New Arrestist & lt; Length & gt; (); = Null scanner; Try {In = new scanner (New file ("src / length / data.txt")); } Hold (FileNotFoundException exception) {new runtime expiration ("Failed to open data"); } // More parts of this assignment require more code, while (in.hasNextDouble ()) {double value = in.nextDouble (); String unit = in.next (); Length = length = faucet; If (unit.equalsIgnoreCase ("m")) {length = new meter (value); Lo.add (length); } And if (unit.equalsIgnoreCase ("foot")) {length = new foot (value); Lo.add (length); } And if (unit.equalsIgnoreCase ("inch")) {length = new inch (value); Lo.add (length); } And if (unit.equalsIgnoreCase ("yard")) {length = new yard (value); Lo.add (length); }} Length [] myLengths = new length [lo.size ()]; // myLengths declares an integer array for (int i = 0; i & lt; lo.size (); i ++) {myLengths [i] = lo .get (i); // copy array, myLengths for length array} (int i = 0; i & lt; myLengths.length; i ++) {system.out.println (myLengths [i] .toString ()); }}}  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -