java - Advancing text in a text file using user input -
so in first year of college christmas project needed make basic game using if statements, chose text adventure me being novice chose write out every print out:
system.out.println("you approach 2 cupboards 1 on right , 1 on left.\nwhich 1 inspect?"); system.out.println("please press 1 inspect right cupboard or 2 press left cupboard."); int action1 = 0; while(action1 !=1 && action1!=2){//checking make sure no 1 puts in number thats not 1 or 2 action1 = in.nextint();//start of player choice if( action1 == 1) { system.out.println("in right cupboard find note.\nupon reading note says:"); system.out.println("hello" + " " + name + " " + "you're wondering why woke in kitchen whith driend blood on shirt unable rememeber anything.\npretty understandable. however, you're part of game , expect play rules. i'm waiting in attic, if can here in 1 piece i'll reveal all.\n ciao"); enter = in.nextline(); system.out.println("after reading note check next cupboard."); system.out.println("it contains small key.\nfar small of locks in kitchen, keep , move on."); enter = in.nextline(); }
but i'm off summer i'm trying study programming myself not rusty, thought it'd idea go game , clean embedding text in text file, theres 1 problem, have absolutely no idea how allow user advance text @ own pace text appears on screen , looks kind of jarring. i'll include sample code in case helps
scanner in = new scanner(system.in); filereader file = new filereader("c:/users/jamie/desktop/textadventure/textfiles/opening.txt"); bufferedreader reader = new bufferedreader(file); string text = ""; //string hold text in text file string line = reader.readline(); while (line != null) { text += line+"\n"; line = reader.readline(); } system.out.println(text);
i'd prefer nudge in right direction instead of full on example i'd figure out myself, little greatful thanks!
type writer approach:
string filename = "file.txt"; list<string> lines = new arraylist<string>(); try (bufferedreader br = new bufferedreader(new filereader(filename))) { string str; while ((str = br.readline()) != null) { lines.add(str); } } catch (ioexception ioe) { //handle errors } while (lines.size() > 0) { //we know lines more 0 remove first element string nextlineout = lines.remove(0); (int charindex = 0; charindex < nextlineout.length(); charindex++) { system.out.print(nextlineout.charat(charindex)); try { //every time character printed console sleep 50 milliseconds thread.sleep(50); } catch (interruptedexception ie) {//handle errors } } //we have reached end of line print out new line system.out.print(system.lineseparator()); }
for print single line , wait x time approach. replace code within
while(lines.length() > 0) { }
with
string nextlineout = lines.remove(0); system.out.println(nextlineout); try { //get amount of characters in next line multiply 50 pause of x milliseconds per line thread.sleep(nextlineout.length() * 50); } catch (interruptedexception ie) {//handle errors }
Comments
Post a Comment