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:

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 ! and where is exactly the wrong in my code ?

Thanks in advance :)

Recommended Answers

All 2 Replies

you're never going into the while loop that is supposed to enter the numbers in your arrays.

take a look at

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

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 {

			File nn = new File(name);
			System.out.println(nn.getAbsolutePath());
			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();
			inputStream.nextLine();
		} else {
			System.out.println("The Size of the matrix is not clearly stated!");
		}

		int array[][] = new int[size][size];
		while (inputStream.hasNextInt()) {
			System.out.println("ran");
			for (int i = 0; i < array.length; i++) {
				for (int j = 0; j < array[i].length; j++) {
					if (inputStream.hasNextInt()) {
						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();
		}
	}

}

, test this and explain to us why I've made those changes and why they are changing your output.

Thanks a lot my friend , it worked.


I understood my mistake because I made only one

inputstream.nextLine();

and I didn't control the while loop properly.

Appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.