Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Creating an adjacency matrix from a file

    Hi guys,

    I am having trouble storing elements from my file into the adjacency matrix, but I feel like I am kinda on the right track. The goal is to read the first line and from that add elements into my row and columns.
    Any help to push me to the right direction is appreciated.
    Thank you.

    My file consists of:
    6
    0 4 4 5 999 999
    4 0 7 8 999 999
    999 7 0 3 7 8
    5 8 3 0 3 999
    999 999 7 3 0 9
    999 999 8 999 9 0


    Java Code:
          // Store the times to loop around
    		int loops = 0;
    		// Sets the size of the array
    		int permanent = 0;
    		System.out.print("Enter filename: ");
    		String fileName = scanner.next();
    		
    		FileReader text = null;
    		try{
    			text = new FileReader(fileName);
    			
    		}catch(FileNotFoundException e){
    			System.out.println("Error opening the file " + fileName);
    			System.exit(0);	
    			
    		}
    		
    		Scanner source = new Scanner(text);
    		source.useDelimiter(" ");
    		
    		// Store elements into this array.
    		int testingArray[][];
    		while(source.hasNext())
    		{
    			loops = Integer.parseInt(source.nextLine());
    			permanent = loops;
    			testingArray = new int[permanent][permanent];
                            //Testing to see if the first element is correct
    			System.out.println(loops);
    			
    			while(loops != 0)
    			{
    				for(int i = 0; i < permanent; i++)
    				{
    					for(int j = 0; j < permanent; j++)
    					{
    						testingArray[i][j] = source.nextInt();
    					}
    					loops--;
    				}
    			}
    		}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating an adjacency matrix from a file

    goal is to read the first line and from that add elements into my row and columns.
    What should the contents of the 2D array look like after the file is read?
    What is in the 2D array when the current code is executed?

    The Arrays class has a useful method: deepToString() for building a String to print a 2D array's contents.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Creating an adjacency matrix from a file

    Quote Originally Posted by Norm
    What should the contents of the 2D array look like after the file is read?
    What is in the 2D array when the current code is executed?

    The Arrays class has a useful method: deepToString() for building a String to print a 2D array's contents.
    When I actually execute it it gives me an error at
    testingArray[i][j] = source.nextInt();
    It states that there is an input mismatch

    The contents of the array should look like this:
    Basically I just want to go through the file and store each element.
    0 4 4 5 999 999
    4 0 7 8 999 999
    999 7 0 3 7 8
    5 8 3 0 3 999
    999 999 7 3 0 9
    999 999 8 999 9 0

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating an adjacency matrix from a file

    it gives me an error at
    testingArray[i][j] = source.nextInt();
    It states that there is an input mismatch
    What is nextInt() reading when that statement is executed?
    Do some debugging by adding some print statements that print out the values of all the variables that are changing value around that statement so you can see what the program is scanning.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Creating an adjacency matrix from a file

    The printing recommendation worked, thanks to that I was able to get it.
    Thanks for your time.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating an adjacency matrix from a file

    Can you explain what the problem was and what you did to fix it so that others reading this thread can understand.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Creating an adjacency matrix from a file

    So what I actually ended up doing is getting rid of the delimiter and inside the inner loop I kept printing out to see what was being stored. The file ended up being stored in the matrix
    Java Code:
                    // Store the times to loop around
    		int loops = 0;
    
    		System.out.print("Enter filename: ");
    		String fileName = scanner.next();
    		
    		FileReader text = null;
    		try{
    			text = new FileReader(fileName);
    			
    		}catch(FileNotFoundException e){
    			System.out.println("Error opening the file " + fileName);
    			System.exit(0);	
    			
    		}
    		
    		Scanner source = new Scanner(text);
    		// Store elements into this array.
    		while(source.hasNext())
    		{
    			loops = Integer.parseInt(source.nextLine());
    			numberOfVertices = loops;
    			testingArray = new int[numberOfVertices][numberOfVertices];
    
    			while(loops != 0)
    			{
    				for(int i = 0; i < numberOfVertices; i++)
    				{	
    					for(int j = 0; j < numberOfVertices; j++)
    					{	
    						testingArray[i][j] = source.nextInt();
    					}
    					loops--;
    				}
    			}
    		}

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Creating an adjacency matrix from a file

    Why do you have that third (outer) loop starting at line #25? The statement at line #33 annihilates the effects of that loop, so it is redundant.

    kind regards

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Creating matrix using a sorted array
    By Omer1233 in forum New To Java
    Replies: 3
    Last Post: 12-22-2013, 08:56 PM
  2. Adjacency Matrix with an ArrayList of ArrayLists
    By clickhere9 in forum New To Java
    Replies: 2
    Last Post: 04-09-2013, 04:59 AM
  3. call matrix on file level
    By speedy8727 in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2012, 11:01 AM
  4. Writing file input in a matrix
    By Neovenator in forum New To Java
    Replies: 50
    Last Post: 01-26-2012, 04:24 PM
  5. Creating a 3x3 matrix of imgs
    By Zarcorp in forum Java Applets
    Replies: 6
    Last Post: 11-25-2010, 01:23 AM

Posting Permissions

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