java - How to solve my try-catch exception handling? -
i'm having problems try-catch exception
here. prompt user name of text file say, robot.txt if file not exist, have make sure application reprompts user file name. hope guys can understand i'm still newbie here please feel free provide suggestions or advices on coding etc. cheers!
main method class:
import java.io.*; import java.util.scanner; import java.util.vector; class testvector3 { public static void main(string [] args) { system.out.println("please enter name of text file read: "); scanner userinput = new scanner(system.in); vector <killerrobot> robotdetails = new vector <killerrobot>(); killerrobot robot; scanner fileinput = null; try { file textfile = new file(userinput.nextline()); fileinput = new scanner(textfile); } catch (filenotfoundexception e) { system.out.println("error - file not found!"); system.out.println("re-enter file name :"); //reprompt user name of text file fileinput = new scanner(userinput.nextline()); } while(fileinput.hasnext()) { robot = new killerrobot(); string first = fileinput.next(); robot.setname(first); string second = fileinput.next(); robot.setmainweapon(second); int third = fileinput.nextint(); robot.setnumberofkills(third); robotdetails.add(robot); } for(killerrobot : robotdetails) { system.out.println(i); } fileinput.close(); } }
killerrobot class file:
class killerrobot { private string name; private string mainweapon; private int numberofkills; killerrobot() { } public string getname() { return name; } public string getmainweapon() { return mainweapon; } public int getnumberofkills() { return numberofkills; } public string tostring() { return name + " used " + mainweapon + " destroy " + numberofkills + " enemies "; } public void setname(string a) { name = a; } public void setmainweapon(string b) { mainweapon = b; } public void setnumberofkills(int c) { numberofkills = c; } }
as state beginner, let first @ relevant part of code, make sure talk same thing:
scanner fileinput = null; try { file textfile = new file(userinput.nextline()); fileinput = new scanner(textfile); } catch (filenotfoundexception e) { system.out.println("error - file not found!"); system.out.println("re-enter file name :"); fileinput = new scanner(userinput.nextline()); }
you have input , want check input condition , require new input until condition fulfilled. problem can solved using loop following:
scanner fileinput = null; { system.out.println("enter file name :"); try { fileinput = new scanner(new file(userinput.nextline())); } catch (filenotfoundexception e) { system.out.println("error - file not found!"); } } while(fileinput == null);
so finally, why work? fileinput
variable set null
, remain null
until given file read standard input because exception thrown otherwise prevents fileinput
variable set. procedure can repeated endlessly.
on side note, performance reasons, not idea implement control flow based on exceptions. better check condition if file exists via file::exists
. however, if read file after checking existence, might have been deleted in meantime introduces racing condition.
answer comment: in java (or programming language), can inline expressions. means instead of calling 2 methods in 2 different statements in
foo foo = method1(); bar bar = method2(foo);
you can call
bar bar = method2(method1());
this way, save space (what becomes more , more important if code gets longer) not need value saved in foo
@ other place in code. similarly, can inline (which how pattern called) from
file file = new file(userinput.nextline()) fileinput = new scanner(file);
into
fileinput = new scanner(new file(userinput.nextline()));
as file
variable read when creating scanner
.
Comments
Post a Comment