java - Having trouble with writing a .txt flie -
so having trouble getting program not throw nosuchelement exceptions @ me. when input owner info, throws exception after second owner no matter owner input:
jones; 221 smith st; arlington; texas; 76019 smith; 7345 lane rd; dallas; texas; 75000 willis; 596 dale lane; fort worth; texas; 76123
here's code:
import java.util.formatter; import java.util.scanner; import java.util.nosuchelementexception; import java.util.stringtokenizer; public class writefile { public static void main(string[] args) { formatter output; try { output = new formatter("owners.txt"); system.out.println("file accessed"); system.out.println("please enter owner information. separate information semicolons please:"); scanner input = new scanner(system.in); while(input.hasnext()) { try { string owner = input.nextline(); string[] tokens = owner.split(";"); output.format("%s %s %s %s %d%n",input.next(), input.next(), input.next(), input.next(),input.nextint()); } catch(nosuchelementexception ee) { system.out.println("error on input. please try again."); input.nextline(); } } output.close(); } catch(exception e) { system.out.println("error working file"); } } }
thank help!
here's problem:
output.format("%s %s %s %s %d%n",input.next(), input.next(), input.next(), input.next(),input.nextint());
the calls read next tokens reading nothing, since earlier input.readline()
has gobbled entire line. should using tokens[]
object in formatter.
Comments
Post a Comment