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.

Results 1 to 5 of 5

Thread: 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

    I'm new to Java, and have been stuck on this project for days. I've exhausted as much time as I'd like, and am now admitting defeat. Would someone who is vastly superior than I give my code a glance... I'm to create a input file with a matrix of numbers. The program will read the file in, detect errors in the surrounding cells, correct the errors, and then output the matrix as a series of symbols. So far, I've gotten the numbers to read in from the input file, but I'm stuck at the detecting errors. Here's the code....(i've included the errors at the bottom)

    import java.io.*;
    import java.util.*;

    public class MatrixFile
    {
    //Instance Data
    private int[][] matrix = new int[30][30];
    private char[][] newMatrix = new char [30][30];
    private int i, j, numRows, numColumns;
    private Scanner fileScan;
    private Scanner fileScan2;
    String rowLine;
    //method to open the input file (file1.txt)
    public MatrixFile()
    {
    try
    {
    fileScan = new Scanner(new File("java3.txt"));
    }
    catch(Exception e)
    {
    System.out.println("could not find file");
    }
    try
    {
    fileScan2 = new Scanner(new File("java3.txt"));
    }
    catch(Exception e)
    {
    System.out.println("could not find file");
    }
    //method to read the file

    int numRow = 0, numColumn = 0;
    String RowLine;
    System.out.println("reading from the file and counting the rows and columns. ");
    while(fileScan.hasNext())
    {
    RowLine = fileScan.nextLine();
    numColumn = RowLine.length() / 2 + 1;
    numRow++;
    }
    System.out.println("The number of columns is " + numColumn);
    System.out.println("The number of rows is " + numRow);
    for(i=0; i<numRow; i++)
    {
    for(j=0; j<numColumn; j++)
    {
    matrix[i][j] = fileScan2.nextInt();
    System.out.print(matrix[i][j]);
    }
    System.out.println();
    }
    fileScan.close();
    }
    public void detectErrors()
    {
    for(int i=0; i<numRows; i++) {
    for(int j=0; j<numColumns; j++)
    {
    if ((i == 0) && (j == 0)) // we are looking at the cell in the upper left corner
    {upperLeft();}
    else if ((i == 0) && (j == numColumns - 1)) // the cell in the upper right corner.
    {upperRight();}
    else if ((i == numRows - 1) && (j == numColumns - 1)) //lower right corner
    {lowerRight();}
    else if ((i == numRows - 1) && (j == 0)) // lower left corner
    {lowerLeft();}
    else if ((i == 0 ) && (j < numColumns - 1)) // upper row
    {upperRow();}
    else if ((i == numRows - 1) && (j < numColumns - 1))//lower row
    {lowerRow();}
    else if ((i < numRows - 1) && (j == numColumns - 1))//left columns
    {leftRow();}
    else if ((i < numRows - 1) && (j == 0))//right columns
    {rightRow();}
    else // we are in the middle
    {middleCell();}
    } }
    }
    public void upperLeft()
    {
    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))
    {matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) / 3);
    //correctUpperLeft();
    }
    }

    public void upperRight()
    {
    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))
    {matrix[i][j]=((matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /3);
    //correctUpperRight();
    }
    }

    public void lowerRight()
    {
    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))
    {matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1]) /3);
    //correctLowerRight();
    }
    }

    public void lowerLeft()
    {
    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))
    {matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j+1]) /3);
    //correctLowerLeft();
    }
    }

    public void upperRow()
    {
    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))
    {matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j] + matrix[i][j-1] + matrix[i+1][j-1]) /5);
    //correctUpperRow();
    }
    }

    public void lowerRow()
    {
    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))
    {matrix[i][j]=((matrix[i][j-1] + matrix[i][j+1] + matrix[i-1][j-1] + matrix[i-1][j+1] + matrix[i-1][j]) /5);

    //correctLowerRow();
    }
    }

    public void rightRow()
    {
    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))
    {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);
    //correctRightRow();
    }
    }

    public void leftRow()
    {
    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))
    {matrix[i][j]=((matrix[i-1][j+1] + matrix[i-1][j] + matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) /5);
    //correctLeftRow();
    }
    }

    public void middleCell(){
    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))
    {matrix[i][j]=((matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j-1] + matrix[i+1][j] + matrix[i+1][j+1]) /8);
    //correctMiddleCell();
    }
    }

    /*public void correctUpperLeft()
    {
    matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) / 3);
    }

    public void correctUpperRight()
    {
    matrix[i][j]=((matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /3);
    }

    public void correctLowerLeft()
    {
    matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j+1]) /3);
    }

    public void correctLowerRight()
    {
    matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1]) /3);
    }

    public void correctUpperRow()
    {
    matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j] + matrix[i][j-1] + matrix[i+1][j-1]) /5);
    }

    public void correctLowerRow()
    {
    matrix[i][j]=((matrix[i][j-1] + matrix[i][j+1] + matrix[i-1][j-1] + matrix[i-1][j+1] + matrix[i-1][j]) /5);
    }

    public void correctMiddleCell()
    {
    matrix[i][j]=((matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j-1] + matrix[i+1][j] + matrix[i+1][j+1]) /8);
    }

    public void correctLeftRow()
    {
    matrix[i][j]=((matrix[i-1][j+1] + matrix[i-1][j] + matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) /5);
    }

    public void correctRightRow()
    {
    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);
    }
    */

    public void createImage()
    {
    for(int i=0; i<numRows; i++) {
    for(int 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] < 8)) {
    newMatrix[i][j] = '+';
    }
    else {
    newMatrix[i][j] = '#';
    }
    System.out.print(newMatrix[i][j]);
    } }
    System.out.println();
    }
    @Override
    public String toString()
    {
    String image = null;
    for(int i=0; i<numRows; i++)
    {
    for(int j=0; j<numColumns; j++)
    {
    image += newMatrix[i][j] + ' ';
    image += "\n";
    }//end for
    }
    return image;


    }// toString

    }

    public class MatrixTester{
    public static void main(String[]args)
    {
    MatrixFile myMatrix = new MatrixFile();
    myMatrix.detectErrors();
    myMatrix.createImage();
    System.out.print(myMatrix);
    }}

    errors I'm getting:
    Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at java.io.PrintStream.write(PrintStream.java:525)
    at java.io.PrintStream.write(PrintStream.java:683)


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

    Default Re: 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

    The posted error message has left off the line that shows where in your program the error happened. Be sure to post all of the text of an error message.

    I'm stuck at the detecting errors.
    Where in the code are the comments that describe what an error is?

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

    --- Update ---

    The posted error message has left off the line that shows where in your program the error happened. Be sure to post all of the text of an error message.

    I'm stuck at the detecting errors.
    Where in the code are the comments that describe what an error is?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

    i've tried some things out, but still get the same errors:
    Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at java.io.PrintStream.write(PrintStream.java:525)
    at java.io.PrintStream.print(PrintStream.java:683)
    at MatrixTester.main(MatrixTester.java:11)



    --- Update ---

    i've tried some things out, but still get the same errors:
    Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at java.io.PrintStream.write(PrintStream.java:525)
    at java.io.PrintStream.print(PrintStream.java:683)
    at MatrixTester.main(MatrixTester.java:11)


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

    Default Re: 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

    Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:157)
    at java.io.PrintStream.write(PrintStream.java:525)
    at java.io.PrintStream.print(PrintStream.java:683)
    at MatrixTester.main(MatrixTester.java:11)
    What variable at line 11 has a null value?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D array project. Takes a matrix of numbers, checks for errors in the surrounding cells, and changes numbers to symbols.

    I figured out what I was doing wrong. I have declared an instance variable, but have the same variable defined in a method. Simple fix.

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. Printing out numbers from an Array.
    By djl1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 13th, 2011, 12:03 PM
  3. [SOLVED] Array random numbers
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2011, 07:05 PM
  4. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  5. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM