Java: FileNotFoundException ends completely -
i've been stuck error 3 hours, because in cse course learned put in "throws filenotfoundexception" in methods in code:
public static void main(string[] args) throws filenotfoundexception { scanner user = new scanner(system.in); intro(); prompt(user); } public static void prompt(scanner user) throws filenotfoundexception { boolean game = true; while(game != false) { system.out.print("(c)reate mad-lib, (v)iew mad-lib, (q)uit? "); string answer = user.next(); answer = answer.touppercase(); if(answer.equals("c")) { create(user); } else if(response == "v") { view(user); } else if(answer.equals("q")) { game = false; } } } public static void create(scanner user) throws filenotfoundexception { system.out.print("input file name: "); string filename = user.nextline(); scanner file = new scanner(new file(filename)); file f = new file(filename); if(!f.exists()) { system.out.print("file not found. try again: "); filename = user.nextline(); f = new file(filename); } system.out.print("output file name: "); printstream ot = new printstream(new file(user.nextline())); filing(user, filename, ot); }
when ran through, , inputting in c: happens.
welcome game of mad libs. ask provide various words , phrases fill in story result written output file (c)reate mad-lib, (v)iew mad-lib, (q)uit? c input file name: exception in thread "main" java.io.filenotfoundexception: (no such file or directory) @ java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(fileinputstream.java:146) @ java.util.scanner.<init>(scanner.java:656) @ madlibs.create(madlibs.java:47) @ madlibs.prompt(madlibs.java:35) @ madlibs.main(madlibs.java:16)
really confused in cse class, , feel did not explain process enough after asking questions. can explain this? thanks.
first of need change "fix" following line:
string answer = user.next();
to read:
string answer = user.nextline();
this means capture newline, meaning won't buffered until next scanner call (preventing reading filepath prompt).
then fixing here too. no need create new scanner, have 1 can use:
system.out.print("input file name: "); string filename = user.nextline(); file f = new file(filename); if(!f.exists()) {
Comments
Post a Comment