Results 1 to 8 of 8
  1. #1
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default 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 !

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default 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.

  3. #3
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default 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 !

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Problem in reading a file into 2-D array

    Print out the numbers as you read them in.

  5. #5
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default 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();
    
        			}
    
    
    
        		}
    
    
        	}

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Problem in reading a file into 2-D array

    it doesn't print anything:
    That means that the code is not being executed.
    What controls if the code in the loop will be executed?
    Print out the condition being tested in the while statement.

  7. #7
    matrixcool is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default Re: Problem in reading a file into 2-D array

    Could you correct my code because I didn't understand your lasr reply

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Problem in reading a file into 2-D array

    What is inside of the ()s in the while statement? It's called a conditional expression.
    Is the expression true or false?
    Write a print statement that prints out its value to see.

Similar Threads

  1. Reading a file into an array?
    By heycoa in forum New To Java
    Replies: 4
    Last Post: 07-07-2011, 04:22 AM
  2. Help with GUI, Array, and Reading File
    By bamagirl31 in forum New To Java
    Replies: 21
    Last Post: 07-05-2011, 01:14 AM
  3. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 08:36 AM
  4. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  5. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •