Friday, July 31, 2009

JAVA Programming Help!!?

Im trying to import a text file using the scanner class into an array. I have the text file that is in an array format (ie first row 4 5 8 3 second row 6 2 3 5, etc.) and I am trying to assign the values by using the scanner.





This is what I have so far to work with:











public static void main (String[] args){


int [][] work = new int [8][8];


int [] Employee = new int[8];


for (int j =0; j%26lt;work[8].length; j++){


for (int i =0; j%26lt;work.length; i++){


Scanner inFile = new Scanner (System.in);


System.out.print("Enter an integer:");


work[j][i]=inFile.nextInt();


Employee[j]+=work[j][i];


}


}











The text file is 7x7 as is the array type seen above. How do I get the Scanner to read from the text file and assign the variables?





Thanks in advance. By the way the file name of the text file is "C:\work.text" thanks....

JAVA Programming Help!!?
The code posted, give or take a bit, should work.





Just compile it:


javac class.java





Then run it and pipe in the file:


java class %26lt; C:\work.txt
Reply:I'm a C++ guy, but I notice that your array and loop is 8x8 but you said the data is 7x7. Is work[8].length a valid reference? I think only work[0..7] are valid. I don't think you want to create a new Scanner for every value, but as I said, I'm a C++ guy.


No comments:

Post a Comment