Results 1 to 8 of 8
- 12-20-2011, 02:58 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
Problem in reading a file into 2-D array
Hello,
I have a file and this how it looks like:
6
{a, b, c, d, e, f}
1 1 1 0 0 0
0 1 1 0 0 0
0 0 1 0 0 0
0 0 1 1 0 0
0 1 1 1 1 0
1 1 1 1 1 1
6 is the number of elements in the set and {a, b, c, d, e, f} is the list of set members and the matrix is the relation matrix.
I'm trying to read the file and store the matrix in 2-D array but when I print the matrix it prints zeros but with the correct size ! and this is my whole code:
Java Code:import java.util.*; import java.io.*; //import java.io.FileInputStream; //import java.io.FileNotFoundException; public class Test1 { public static void main(String[] args) { Scanner key= new Scanner (System.in); System.out.println("Enter the name of the file: "); String name=key.next(); Scanner inputStream=null; try{ inputStream= new Scanner(new FileInputStream(name)); } catch(FileNotFoundException e) { System.out.println("File"+name+" is not found!"); System.out.println("or couldn't be opened!"); System.exit(0); } int size=0; if (inputStream.hasNext()){ size= inputStream.nextInt(); inputStream.nextLine(); } else{ System.out.println("The Size of the matrix is not clearly stated!"); } int array[][]=new int[size][size]; while (inputStream.hasNextInt()){ for (int i=0;i<array.length;i++){ for (int j=0;j<array[i].length;j++){ array[i][j]=inputStream.nextInt(); inputStream.next(); } } } inputStream.close(); for (int i = 0; i < array.length; i++) { for (int j = 0; j <array[i].length; j++){ System.out.print(array[i][j]); System.out.print(" "); } System.out.println(); } } }
Could any one please tell me why the output is zeros but the size of the matrix is correct !
- 12-20-2011, 03:00 AM #2
Re: Problem in reading a file into 2-D array
Try debugging your code by adding printlns to show that you are reading in any numbers. Print out the numbers as you read them in.
- 12-20-2011, 03:02 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
Re: Problem in reading a file into 2-D array
I read the file into 2-D array and then printed it but the output is turned out to be zeros !
- 12-20-2011, 03:04 AM #4
Re: Problem in reading a file into 2-D array
Print out the numbers as you read them in.
- 12-20-2011, 03:13 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
Re: Problem in reading a file into 2-D array
I tried this code and it doesn't print anything:
Java Code:while (inputStream.hasNextInt()){ for (int i=0;i<array.length;i++){ for (int j=0;j<array[i].length;j++){ array[i][j]=inputStream.nextInt(); System.out.print(array[i][j] + " "); inputStream.next(); } } }
- 12-20-2011, 03:15 AM #6
Re: Problem in reading a file into 2-D array
That means that the code is not being executed.it doesn't print anything:
What controls if the code in the loop will be executed?
Print out the condition being tested in the while statement.
- 12-20-2011, 03:20 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
Re: Problem in reading a file into 2-D array
Could you correct my code because I didn't understand your lasr reply
- 12-20-2011, 03:34 AM #8
Similar Threads
-
Reading a file into an array?
By heycoa in forum New To JavaReplies: 4Last Post: 07-07-2011, 04:22 AM -
Help with GUI, Array, and Reading File
By bamagirl31 in forum New To JavaReplies: 21Last Post: 07-05-2011, 01:14 AM -
Reading csv file into 2D array - HELP!!!
By mikeg in forum New To JavaReplies: 17Last Post: 04-12-2011, 08:36 AM -
Problem reading a text file into a two-dimensional character array
By wvu5004 in forum New To JavaReplies: 5Last Post: 03-12-2011, 01:04 AM -
Help with reading from file into an array
By Trad in forum New To JavaReplies: 3Last Post: 10-22-2010, 12:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks