java - I am having troubles reading text into an array, what do I need to change in this code? -
this current text file trying read from:
buffalo montreal boston ottawa toronto whenever run code, 5 elements of array print "null", somehow not storing information array properly. here code have far! (i using ready program ide)
// "hockey" class. import java.awt.*; import hsa.console; import java.io.*; public class hockey { static console c; // output console public static void main (string[] args) throws ioexception { c = new console (); //setting file reading filereader fr = new filereader ("cities.txt"); bufferedreader br = new bufferedreader (fr); //initialising array string cities[] = new string [5]; //loop read in 5 entries (int = 0 ; > cities.length ; i++) { cities [i] = br.readline (); } //loop print elements of "cities" console (int = 0 ; < cities.length ; i++) { c.println (cities [i]); } // place program here. 'c' output console } // main method } // hockey class thanks of help!
your loop has incorrect condition.
//loop read in 5 entries (int = 0 ; > cities.length ; i++) //reads (i greater cities.length) { ^^^^^^^^^^^^^^^^^ cities [i] = br.readline (); } the above should be: i < cities.length;(i less cities.length)
the loop isn't running, explains why array isn't populating.
Comments
Post a Comment