Wednesday 15 May 2013

java - I can't read a number in a text file -



java - I can't read a number in a text file -

i want open file, reads strings representing numbers of type int, , set them in order wont read int n6 = inputstream.nextint( );. 1 time take away int n6 code, works. code in main, know 6th 1 isn't in right place, show won't work. i'm using netbeans if helps.

the numbers are: 5, -3, 3, 8, 4, 0

import java.io.fileinputstream; import java.io.filenotfoundexception; import java.util.scanner; import java.util.nosuchelementexception; scanner inputstream = null; seek { inputstream = new scanner(new fileinputstream("stuff.txt")); } catch(filenotfoundexception e) { system.out.println("file not found"); system.exit(0); } catch(nosuchelementexception e) { system.out.println("error"); system.exit(0); } int n1 = inputstream.nextint( ); int n2 = inputstream.nextint( ); int n3 = inputstream.nextint( ); int n4 = inputstream.nextint( ); int n5 = inputstream.nextint( ); int n6 = inputstream.nextint( ); inputstream.nextline(); string line = inputstream.nextline(); system.out.println(n2); system.out.println(n1); system.out.println(n3); system.out.println(n5); system.out.println(n4); system.out.println(n6); inputstream.close();

i suggest not using nextline() , using code snippet:

scanner scanner = new scanner(new file("containsints.txt")); int [] containsints = new int [100]; int = 0; while(scanner.hasnextint()) { containsints[i++] = scanner.nextint(); }

i hope helps, had same problem few weeks ago lol

java java.util.scanner

No comments:

Post a Comment