java - Getting InputMismatchException When Reading Text File into Integer Array -
i reading text file filled numbers (on individual lines) integer array. thought wrote program having issues when try run it. netbeans telling me "inputmismatchexception." think problem located in second method called readtxtfile while loop. appreciated.
package arrayspa; import java.util.scanner; public class arrayspa { /** * using enrollment.txt file, count , display number of full * sections , percentage of sections full. section * full if contains 36 students. */ public static void main(string[] args) throws exception { //an array hold total # of students in each section int[] numstud = new int[100]; int count; //number of elements used int fullsections; //number of full sections (36 enrolled students) int percent; //percentage of sections full //read data numstud[] txt file count = readtxtfile(numstud); //print array on screen system.out.println("the original file:"); displaylines(numstud,count); //calculate number of sections full , display number fullsections = calcfullsections(numstud, count); system.out.println("there "+fullsections+ "full sections."); //display percentage of sections full percent = fullsections/count; system.out.println("the percentage of sections full " +percent); } //end main() /** * methods read data enrollment.txt (located in project folder) * line line integer array. uses if statement * display total number of full sections (a section considered full * if there 36 students enrolled). */ public static int readtxtfile(int[] numstud) throws exception { int i=0; //number of elements in array initialized 0 //create file class object linked enrollment.txt java.io.file enrollment = new java.io.file("enrollment.txt"); //create scanner named infile read input stream file scanner infile = new scanner(enrollment); /**create while loop read lines of text array. uses *scanner class boolean function hasnextline() see if there *another line in file. */ while (infile.hasnextline()) { //read line , put in array element numstud[i] = infile.nextint(); ++; //increment number of array elements } //end while infile.close(); return i; //returns number of items used in array } //end readtxtfile(int[] numstud public static void displaylines(int[] lines, int count) { int i; //loop counter // iterate elements used (i=0; < count; i++) system.out.println(lines[i]); } //end displaylines() public static int calcfullsections(int[] numstud, int count) { int fullsections=0; //number of full sections int i; //loop counter (i=0; < count; i++) if (numstud[i]==36) { fullsections = fullsections + 1; } return fullsections; } //end calcfullsections() }
an inputmismatchexception thrown when input not match type of variable writing to. have tested code , seemed work fine unless file "enrollment.txt" has blank line or spaces. have tested adding blank line end of file , running it, , received nosuchelementexception, problem blank line or non-integer somewhere in file. fix this, can either remove blank lines/non-integers in file, or, preferably, can modify readtxtfile() method ignore blank lines catching nosuchelementexception, so:
public static int readtxtfile(int[] numstud) throws exception { int i=0; //number of elements in array initialized 0 //create file class object linked enrollment.txt java.io.file enrollment = new java.io.file("enrollment.txt"); //create scanner named infile read input stream file scanner infile = new scanner(enrollment); /**create while loop read lines of text array. uses *scanner class boolean function hasnextline() see if there *another line in file. */ while (infile.hasnextline()) { // add try/catch block prevent reading blank line try { numstud[i] = infile.nextint(); ++; } catch (nosuchelementexception e) { } } //end while infile.close(); return i; //returns number of items used in array } //end readtxtfile(int[] numstud as can see, have added try/catch block readtxtfile() void catches nosuchelementexception, includes inputmismatchexception, preventing attempt add in non-integers. hope solves problem!
Comments
Post a Comment