java - Load Text file into array -
im writing hotel console program, problem have @ moment load saved file string[], when user presses option load file.
the text file includes guest names saved earlier.
here file data
tom mo jo john meg bob jack veronica jessica angelica and here code have
yes thank know arrays 0 index. loops starting 1 because want have room1 instead room0 first
thank problem solved
public class fileload { public string[] readlines(string filename) throws ioexception { filereader filereader = new filereader(filename); bufferedreader bufferedreader = new bufferedreader(filereader); list<string> lines = new arraylist<string>(); string line = null; while ((line = bufferedreader.readline()) != null) { lines.add(line); } bufferedreader.close(); return lines.toarray(new string[lines.size()]); } public class hotel_array { if (menu.equalsignorecase("s")) { save(hotel); } if (menu.equalsignorecase("l")) { load(hotel); } } } private static void save(string hotel[]) { try { printwriter pr = new printwriter("data.txt"); (int = 1; < 11; i++) { pr.println(hotel[i]); } pr.close(); } catch (exception e) { e.printstacktrace(); system.out.println("no such file exists."); } } public static void load(string[] args) { fileload rf = new fileload(); string file = "data.txt"; try { string[] hotel = rf.readlines(file); (string line : hotel) { system.out.println(line); // prints file not loads array } } catch (ioexception e) { system.out.println("unable create " + file + ": " + e.getmessage()); } } }
you change fileload class , add method write array file, keep file io in 1 class.
public class fileload { public static string[] readhotelarray(string filename) throws ioexception { bufferedreader bufferedreader = new bufferedreader(new filereader(filename)); list<string> lines = new arraylist<string>(); string line = null; while ((line = bufferedreader.readline()) != null) { lines.add(line); } bufferedreader.close(); return lines.toarray(new string[lines.size()]); } public static void writehotelarray(string filename, string[] hotel) throws ioexception { bufferedwriter bufferedwriter = new bufferedwriter(new filewriter(filename, false)); //write each string array file new line (string s : hotel) bufferedwriter.write(s + "\n"); bufferedwriter.flush(); bufferedwriter.close(); } } note: both methods static don't have instantiate new object since there 1 method call on object
now have change way save , load array in hotel_array class. use this:
//... private static void save(string[] hotel) { try { fileload.writehotelarray("data.txt", hotel); } catch (exception e) { e.printstacktrace(); system.out.println("no such file exists."); } } public static string[] load() { string file = "data.txt"; string[] hotelarray = null; try { hotelarray = fileload.readhotelarray(file); } catch (ioexception e) { system.out.println("unable create " + file + ": " + e.getmessage()); } return hotelarray; } //... and since parameters in java pass-by-value (more here) need return string array in load() method. , therefore have change tiny bit of code in main method.
from:
//... if (menu.equalsignorecase("l")) { load(hotel); } //... to:
//... if (menu.equalsignorecase("l")) { hotel = load(); } //... hope helps bit (:
Comments
Post a Comment