Reading from Input Files in Java -


i've been trying figure out how code, can't figure out how change current code work.

the problem reads:

write program read integer n (2 <= n <= 30) input filename “input5_01.txt” , write function create n x n matrix random integers range [10, 99]. output matrix output filename “output5_01.txt”

currently, here's code:

public static final int upper = 99, lower = 10;  public static void main(string[] args) throws filenotfoundexception {     int n = 5;     int[][] arr = new int[n][n];     scanner inputfile = new scanner(new file("input5_01.txt"));        //read in int (n) input file     while (inputfile.hasnext()) {         if (inputfile.hasnextint() >= 2 && inputfile.hasnextint() <= 30) {             n = inputfile.nextint();         } else {             inputfile.next();         }     }       //assigning random numbers array     random rand = new random();     (int r = 0; r < arr.length; r++) {         (int c = 0; c < arr[0].length; c++) {             arr[r][c] = rand.nextint(upper - lower + 1) + lower;         }         system.out.println();     } } 

really, problem have is:

if(inputfile.hasnextint() >= 2 && inputfile.hasnextint() <= 30) 

what can make work?

as per javadoc, hasnextint() returns boolean, denoting if there integer value or not within stream, not return value of next integer. thus, section:

while(inputfile.hasnext()){   if(inputfile.hasnextint() >= 2 && inputfile.hasnextint() <= 30)     n = inputfile.nextint();      else     inputfile.next(); } 

would need read so:

int nextint = -1; boolean intfound = false;  while(inputfile.hasnext()){   if(inputfile.hasnextint()) {       nextint = inputfile.nextint();       if((nextint >= 2) && (nextint <= 30) {           intfound = true;           break;       }   }    }  if(intfound) {      ... //populate matrix according value of nextint } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -