java - Error catching and File IO -
i have assignment need write while loop code block contains trycatch
statement. try
block retrieves each line input file, , invokes isvalid
method created check if format correct, passing line file. if there no more lines parse, runprogram
set false
, while loop terminates. catch
block catch exception made. far have
public static void main(string[] args) { file file; scanner inputfile; string filename; scanner scan = new scanner(file); filename = scan.nextline(); boolean runprogram = true; while(runprogram) { try { // loop check each line of file // invoke isvalid // check if it's last line in file, , end program if } catch(bankaccountexception e) { system.out.println("account exception. wish quit? y/n"); string quit = scan.nextline(); if(quit.equals("y")) runprogram = false; else runprogram = true; } } }
i have no idea how open file, check next line, use isvalid
method (which stringtokenizer
checks correct format), , closes when reaches end of file.
here isvalid
method:
private static boolean isvalid(string accountline) throws bankaccountexception { stringtokenizer strtok = new stringtokenizer(accountline, ";"); boolean valid = true; if(strtok.counttokens() == 2) { if(strtok.nexttoken().length() == 10) { if(!strtok.nexttoken().matches(".*[0-9].*")) { valid = true; } } } else valid = false; return valid; }
i have question above method. if call .nexttoken()
twice, right in expecting first iteration deal first token, , second deal second? or both check first token?
just started.
try { bufferedreader reader = new bufferedreader(new filereader(new file("/path/to/file"))); string currline; while ((currline = reader.readline()) != null) { // returns null @ eof if (!isvalid(currline)) throw new bankaccountexception(); } } catch (bankaccountexception e) { // same } { reader.close(); }
Comments
Post a Comment