Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Problem with Two-Dimensional Array Program

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Two-Dimensional Array Program

    This is a homework assignment for my csci 1302 class. The assignment is for the program to read a matrix of numbers from an input file (provided by the user), count up the number of rows and columns in the matrix, store the matrix into an array, go through the array and check for any errors (an error is defined as any number that differs by more than 1 from all of its neighbors), correct any errors (corrections made by taking the average of all its neighbors), and after correcting all errors it goes through the corrected matrix and changes each number to a character in order to print an image based on the matrix. Both the matrix class and the driver program have been completely debugged. When I execute the driver program, the only thing that runs is the constructor. None of the methods printed afterward will run. PLEASE HELP!

    This is the input file:
    1 2 1 4 5 3 6 8 0
    2 4 3 6 2 6 4 8 3
    5 1 3 6 8 1 4 3 5
    6 8 4 5 9 7 3 1 2
    8 6 5 4 9 7 3 1 2
    5 6 8 4 3 2 1 8 9
    5 8 4 3 1 2 1 4 5
    8 5 6 4 7 4 5 6 4
    9 8 4 5 3 2 4 5 7


    This is the matrix class:
    import java.util.*;
    import java.io.*;
     
    public class MatrixFile
    {
    	private int[][] matrix = new int[30][30];
    	private String[][] newMatrix = new String[30][30]; 
    	private int i, j, numRows, numColumns;
    	private String rowLine;
     
    	public MatrixFile() throws IOException
    	{
    		Scanner fileScan = new Scanner(new File("file1.txt"));
    		int numRows = 0, numColumns = 0;
    		String rowLine;
    		System.out.println("reading from the file and counting the rows and columns");
    			while (fileScan.hasNext())
    			{
    				rowLine = (fileScan.nextLine());
    				numColumns = (rowLine.length()/2+1);
    				numRows++;				
    			}
    			System.out.println("The number of columns is: " + numColumns);
    			System.out.println("The number of rows is: " + numRows);
    		/******************************************************************/
    		Scanner fileScan2 = new Scanner(new File("file1.txt"));
    		for (i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				matrix[i][j] = fileScan2.nextInt();
    				System.out.print(matrix[i][j]);
    			}
    			System.out.println();
    		}
    	}
     
    	public void detectErrors()
    	{
    		for (i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				if ((i==0)&&(j==0))
    					checkUpperLeft();
    				else if ((i==0)&&(j==(numColumns-1)))
    					checkUpperRight();
    				else if ((i<(numRows-1))&&(j==0))
    					checkLeft();
    				else if ((i<(numRows-1))&&(j==(numColumns-1)))
    					checkRight();
    				else if ((i==(numRows-1))&&(j==0))
    					checkLowerLeft();
    				else if ((i==(numRows-1))&&(j==(numColumns-1)))
    					checkLowerRight();
    				else if ((i==0)&&(j<(numColumns-1)))
    					checkUpperRow();
    				else if ((i<(numRows-1))&&(j<(numColumns-1)))
    					checkMiddle();
    				else if ((i==(numRows-1))&&(j<(numColumns-1)))
    					checkLowerRow();
     
    				System.out.print(matrix[i][j]);	
    			}
    			System.out.println();
    		}
    	}
     
    	private void checkUpperLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{
    			correctUpperLeft();
    		}
     
    	}
     
    	private void checkUpperRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{
    			correctUpperRight();
    		}
    	}
     
    	private void checkLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&((Math.abs(matrix[i][j]-matrix[i][j+1]))>1)&&((Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)))
    		{
    				correctLeft();
    		}
    	}
     
    	private void checkRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{	
    			correctRight();
    		}
    	}
     
    	private void checkLowerLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1))
    		{
    			correctLowerLeft();
    		}
    	}
     
    	private void checkLowerRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1))
    		{
    			correctLowerRight();
    		}
    	}
     
    	private void checkMiddle()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1))
    		{
    			correctMiddle();
    		}
    	}
     
    	private void checkUpperRow()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1))
    		{	
    			correctUpperRow();
    		}
    	}
     
    	private void checkLowerRow()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1));
    		{	
    			correctLowerRow();
    		}
    	}
     
    	private void correctUpperLeft()
    	{
    		matrix[i][j]=((matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/3);
    	}
     
    	private void correctUpperRight()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i-1][j])/3);
    	}
     
    	private void correctLeft()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/5);
    	}
     
    	private void correctRight()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j])/5);
    	}
     
    	private void correctLowerLeft()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/3);
    	}
     
    	private void correctLowerRight()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1])/3);
    	}
     
    	private void correctMiddle()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j]+matrix[i+1][j-1]
    							+matrix[i][j-1]+matrix[i-1][j-1])/8);
    	}
     
    	private void correctUpperRow()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j]+matrix[i+1][j+1]+matrix[i][j+1])/5);
    	}
     
    	private void correctLowerRow()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i-1][j-1]+matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/5);
    	}
     
    	public void createImage()
    	{
    		for(i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				if ((matrix[i][j]>=0)&&(matrix[i][j]<3))
    					newMatrix[i][j]=(" ");
    				else if ((matrix[i][j]>=3)&&(matrix[i][j]<6))
    					newMatrix[i][j]=(".");
    				else if ((matrix[i][j]>=6)&&(matrix[i][j]<9))
    					newMatrix[i][j]=("+");
    				else if ((matrix[i][j]==9))
    					newMatrix[i][j]=("#");
    			}
    		}
    	}
     
    	public String toString()
    	{
    		String image;
    		image="";
    		for(i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				image += (newMatrix[i][j] + " ");				
    			}
    			image += ("\n");
    		}
     
    		return image;
    	}
    }


    This is the driver program:
    import java.io.*;
     
    public class MatrixFileTester
    {
    	public static void main(String[] args) throws IOException
    	{
    		MatrixFile myMatrix = new MatrixFile();
    		System.out.println();
    		myMatrix.detectErrors();
     
    		myMatrix.createImage();
     
    		System.out.print(myMatrix);
    	}
    }


    And here is my output:
    ----jGRASP exec: java MatrixFileTester

    reading from the file and counting the rows and columns
    The number of columns is: 9
    The number of rows is: 9
    121453680
    243626483
    513681435
    684597312
    865497312
    568432189
    584312145
    856474564
    984532457


    ----jGRASP: operation complete.
    Last edited by soradogoof; February 20th, 2012 at 10:32 AM.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Um. I'm not sure why in the correction private methods it changed all the equal signs to broken heart faces but yeah...those are equal signs.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    What text is next to the = that make the faces?

    the only thing that runs is the constructor
    Where do you call any of the methods?
    Last edited by Norm; February 17th, 2012 at 01:07 PM.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Oh. an open parenthese makes the faces. That makes sense. Well. Not much I can do about that. and I'll get the highlighting right real quick.
    And I called all the methods in the driver program myMatrix.detectErrors(), myMatrix.createImage(), etc.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Not much I can do about that.
    put spaces around the =

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Well after I put it in the java code format the smileys don't appear anyway so...

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    If nothing is printed by the methods that are being called from the main method, then add some more println statements to those methods to show the values of the variables that control the logic in those methods. The values printed out will show you what the program is doing.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    I have added println statements to the detectErrors method and discovered hat for some reason, the for loop is not being entered. I am posting this reply with my phone so as soon as I get home I will post my updated code and output so maybe someone can see why the for loop is working at all.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Not working at all*

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Print out the values of ALL variables used in those methods to see why the for loops are not entered.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Here's my updated matrix class: (i only updated the detectErrors method, i will take correcting the methods one method at a time for easier thought-process. although hopefully whatever is the issue with one method is the same for all.)

    import java.util.*;
    import java.io.*;
     
    public class MatrixFile
    {
    	private int[][] matrix = new int[30][30];
    	private String[][] newMatrix = new String[30][30]; 
    	private int i, j, numRows, numColumns;
    	private String rowLine;
     
    	public MatrixFile() throws IOException
    	{
    		Scanner fileScan = new Scanner(new File("file1.txt"));
    		int numRows = 0, numColumns = 0;
    		String rowLine;
    		System.out.println("reading from the file and counting the rows and columns");
    			while (fileScan.hasNext())
    			{
    				rowLine = (fileScan.nextLine());
    				numColumns = (rowLine.length()/2+1);
    				numRows++;				
    			}
    			System.out.println("The number of columns is: " + numColumns);
    			System.out.println("The number of rows is: " + numRows);
    		/******************************************************************/
    		Scanner fileScan2 = new Scanner(new File("file1.txt"));
    		for (i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				matrix[i][j] = fileScan2.nextInt();
    				System.out.print(matrix[i][j]);
    			}
    			System.out.println();
    		}
    	}
     
    	public void detectErrors()
    	{
    		System.out.println("checking for and correcting errors");
    		System.out.println("i = " + i + " j = " + j);
    		for (i=0; i<numRows; i++)
    		{
    			System.out.println("i = " + i);
    			for (j=0; j<numColumns; j++)
    			{
    				System.out.println("i = " + i + "j = " + j);
    				if ((i==0)&&(j==0))
    				{
    					System.out.println("checking upper left");
    					checkUpperLeft();
    				}
    				else if ((i==0)&&(j==(numColumns-1)))
    					checkUpperRight();
    				else if ((i<(numRows-1))&&(j==0))
    					checkLeft();
    				else if ((i<(numRows-1))&&(j==(numColumns-1)))
    					checkRight();
    				else if ((i==(numRows-1))&&(j==0))
    					checkLowerLeft();
    				else if ((i==(numRows-1))&&(j==(numColumns-1)))
    					checkLowerRight();
    				else if ((i==0)&&(j<(numColumns-1)))
    					checkUpperRow();
    				else if ((i<(numRows-1))&&(j<(numColumns-1)))
    					checkMiddle();
    				else if ((i==(numRows-1))&&(j<(numColumns-1)))
    					checkLowerRow();
     
    				System.out.print(matrix[i][j]);	
    			}
    			System.out.println();
    		}
    	}
     
    	private void checkUpperLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{
    			correctUpperLeft();
    		}
     
    	}
     
    	private void checkUpperRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{
    			correctUpperRight();
    		}
    	}
     
    	private void checkLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&((Math.abs(matrix[i][j]-matrix[i][j+1]))>1)&&((Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)))
    		{
    				correctLeft();
    		}
    	}
     
    	private void checkRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))
    		{	
    			correctRight();
    		}
    	}
     
    	private void checkLowerLeft()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1))
    		{
    			correctLowerLeft();
    		}
    	}
     
    	private void checkLowerRight()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1))
    		{
    			correctLowerRight();
    		}
    	}
     
    	private void checkMiddle()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1))
    		{
    			correctMiddle();
    		}
    	}
     
    	private void checkUpperRow()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1))
    		{	
    			correctUpperRow();
    		}
    	}
     
    	private void checkLowerRow()
    	{
    		if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1)
    			&&(Math.abs(matrix[i][j]-matrix[i][j+1])>1));
    		{	
    			correctLowerRow();
    		}
    	}
     
    	private void correctUpperLeft()
    	{
    		matrix[i][j]=((matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/3);
    	}
     
    	private void correctUpperRight()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i-1][j])/3);
    	}
     
    	private void correctLeft()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/5);
    	}
     
    	private void correctRight()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j])/5);
    	}
     
    	private void correctLowerLeft()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/3);
    	}
     
    	private void correctLowerRight()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1])/3);
    	}
     
    	private void correctMiddle()
    	{
    		matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j]+matrix[i+1][j-1]
    							+matrix[i][j-1]+matrix[i-1][j-1])/8);
    	}
     
    	private void correctUpperRow()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j]+matrix[i+1][j+1]+matrix[i][j+1])/5);
    	}
     
    	private void correctLowerRow()
    	{
    		matrix[i][j]=((matrix[i][j-1]+matrix[i-1][j-1]+matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/5);
    	}
     
    	public void createImage()
    	{
    		for(i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				if ((matrix[i][j]>=0)&&(matrix[i][j]<3))
    					newMatrix[i][j]=(" ");
    				else if ((matrix[i][j]>=3)&&(matrix[i][j]<6))
    					newMatrix[i][j]=(".");
    				else if ((matrix[i][j]>=6)&&(matrix[i][j]<9))
    					newMatrix[i][j]=("+");
    				else if ((matrix[i][j]==9))
    					newMatrix[i][j]=("#");
    			}
    		}
    	}
     
    	public String toString()
    	{
    		String image;
    		image="";
    		for(i=0; i<numRows; i++)
    		{
    			for (j=0; j<numColumns; j++)
    			{
    				image += (newMatrix[i][j] + " ");				
    			}
    			image += ("\n");
    		}
     
    		return image;
    	}
    }


    Here is the driver program again:
    import java.io.*;
     
    public class MatrixFileTester
    {
    	public static void main(String[] args) throws IOException
    	{
    		MatrixFile myMatrix = new MatrixFile();
    		System.out.println();
    		myMatrix.detectErrors();
     
    		myMatrix.createImage();
     
    		System.out.print(myMatrix);
    	}
    }

    and here is the new output:

    ----jGRASP exec: java MatrixFileTester

    reading from the file and counting the rows and columns
    The number of columns is: 9
    The number of rows is: 9
    121453680
    243626483
    513681435
    684597312
    865497312
    568432189
    584312145
    856474564
    984532457

    checking for and correcting errors
    i = 9 j = 9

    ----jGRASP: operation complete.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Have you found the problem yet?
    I see that You don't print out ALL of the variables' values. You need to print out the values of ALL variables used in those methods to see why the for loops are not entered.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    I still can't find the problem. The only variable I missed that that method used was the array matrix[][] . Which I have added a print statement for and I have the same issue. It is equal to 0 before entering the loop and the print statement inside the loop does not execute. I also added a print statements at the end of the method outside of the for loops which also work correctly. I am completely stumped.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Did you print out ALL the variables used to control the looping in the methods?
    Your code shows print outs for i & j
    What about:
    for (i=0; i<numRows; i++)

  15. #15
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Ah. There's the problem. In the method numRows is equal to 0. But it gets set to 9 in the constructor. Why does it reset?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Use your editor's find to look at ALL the places numRows is coded.
    Where is it defined? Where is it given values?

  17. #17
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    It is declared at the beginning of the class, set to 0 at the beginning of the constructor, and then set to 9 after the constructor reads the input file. After that, it is accessed multiple times but never again. ???

  18. #18
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Never again altered*

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Where is it defined?
    Do you know the syntax for defining a variable?
    <datatype> <variable name>
    How many places is there a statement that starts with:
    int and has numRows somewhere to the right?

  20. #20
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Idk what happened to abunch of the posts but numRows is not altered after it gets set to 9 in the constructor. Accessed alot, but not altered. ?????

  21. #21
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    It is defined in the beginning of the program and inside the constructor. That's all.

  22. #22
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    i took away the definition inside the constructor and that got it going. Now I'm getting and exception found error
    Here is my new output:

    ----jGRASP exec: java MatrixFileTester

    reading from the file and counting the rows and columns
    The number of columns is: 9
    The number of rows is: 9
    121453680
    243626483
    513681435
    684597312
    865497312
    568432189
    584312145
    856474564
    984532457

    checking for and correcting errors
    i = 9 j = 9
    0
    9
    0
    i = 0
    i = 0j = 0
    checking upper left
    1i = 0j = 1
    2i = 0j = 2
    1i = 0j = 3
    4i = 0j = 4
    5i = 0j = 5
    3i = 0j = 6
    6i = 0j = 7
    8i = 0j = 8
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at MatrixFile.correctUpperRight(MatrixFile.java:175)
    at MatrixFile.checkUpperRight(MatrixFile.java:95)
    at MatrixFile.detectErrors(MatrixFile.java:58)
    at MatrixFileTester.main(MatrixFileTester.java:9)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    It is defined in the beginning of the program and inside the constructor.
    That means there are two separate, independent variables. Having the same name does not connect them in any way.
    The one in the constructor goes away when the constructor exits.
    When do you give values to the one outside of the constructor?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at MatrixFile.correctUpperRight(MatrixFile.java:175)
    Why is the value of the index less than 0 (-1) at line 175?
    Where do you set its value? Again printlns will help you find where the logic is going wrong.

  25. #25
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Two-Dimensional Array Program

    Anddd solved! Thank you very much!

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] 2 dimensional array storing help!
    By jts0541 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2012, 03:31 PM
  2. Input in a two Dimensional Array.
    By Mr.777 in forum File I/O & Other I/O Streams
    Replies: 29
    Last Post: December 14th, 2011, 11:40 PM
  3. 2 dimensional array coding
    By Bighairjersey in forum Java Theory & Questions
    Replies: 8
    Last Post: July 22nd, 2011, 02:30 PM
  4. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM