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 24 of 24

Thread: Error displaying matrix?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Error displaying matrix?

    Im getting an exception in my display method.
    I need to be able to display to the console the rows and columns of the matrix.
    Heres the matrix I want to display:

    import java.util.Scanner;

    public class MatrixMain {

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int [][] constMatrix = { {1, 2, 3},
    {4, 5, 6}
    };
    Matrix m1 = new Matrix(constMatrix);
    System.out.printf("Constant matrix (%d x %d):\n",
    m1.rows(), m1.columns());
    m1.display();

    And here is my method:

    import java.util.Random;
    public class Matrix {

    private int[][] matrix;

    public Matrix(int[][] m) {
    matrix = new int[m.length][m[0].length];
    int row = m.length;
    int col = m[0].length;
    for (int i = 0; i < row; i++)
    for (int j = 0; j < col; j++)
    this.matrix[i][j] = m[i][j];
    }

    public int rows() {
    return matrix.length;
    }

    public int columns() {
    return matrix[0].length;
    }

    public void display() {
    for (int i = 0; i < rows(); i++)
    for (int j = 0; j < columns(); j++)
    System.out.printf("%9.4f", matrix[i][j]);
    System.out.println();
    }

    The exception points to this bolded line of code.
    Thanks for any help!


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

    Default Re: Error displaying matrix?

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [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
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    I need to be able to display to the console the rows and columns of the matrix.
    Heres the matrix I want to display in the same format:

    import java.util.Scanner;
     
    public class MatrixMain {
     
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
     
    int [][] constMatrix = { {1, 2, 3}, 
                             {4, 5, 6} 
                                  };
    Matrix m1 = new Matrix(constMatrix);
    System.out.printf("Constant matrix (%d x %d):\n",
    m1.rows(), m1.columns());
    m1.display();

    And here is my method:

    import java.util.Random;
    public class Matrix {
     
    private int[][] matrix;
     
    public Matrix(int[][] m) {
    matrix = new int[m.length][m[0].length];
    int row = m.length;
    int col = m[0].length;
    for (int i = 0; i < row; i++)
    for (int j = 0; j < col; j++)
    this.matrix[i][j] = m[i][j];
    }
     
    public int rows() {
    return matrix.length;
    }
     
    public int columns() {
    return matrix[0].length;
    }
     
    public void display() {
    for (int i = 0; i < rows(); i++)
    for (int j = 0; j < columns(); j++)
    System.out.printf("%d", matrix[i][j]);
    System.out.println();
    }


    This outputs 123456 with no exception but its not the format I need.
    I also need to allow for any size matrix the user inputs later in my code.
    Thanks for any help!

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error displaying matrix?

    Always include the full text of the error message with your code and question

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    There is no error in this code. I just tried something else and now Im getting 123456 with no error/exception.

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

    Default Re: Error displaying matrix?

    This outputs 123456 with no exception but its not the format I need.
    What is the format that you need? Give an example.

    One problem I see with the code is the missing {} following each for statement. Always use {}s to surround the statements that are in the for loop.

    Also the code is not properly formatted. Nested statements should be indented 3-4 spaces. All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error displaying matrix?

    If you want to print a new line on the end of each row you will have to determine when you have printed the last element of that row, and then print a new line. What part are you having problems with?

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    I need it to have the format:

    1 2 3
    4 5 6

    with tab spacing.

    And allow for any size matrix the user might input. This is just a simple 2 x 3 example.

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

    Default Re: Error displaying matrix?

    One problem I see with the code is the missing {} following each for statement. Always use {}s to surround the statements that are in the for loop.

    Also the code is not properly formatted. Nested statements should be indented 3-4 spaces. All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    My code is properly formatted it just didnt preserve it when I copy/pasted to this forum from Eclipse. All the brackets and statements are where they should be.
    Thanks!

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Error displaying matrix?

    The white spaces were removed when you submitted the post without code tags in place. If you copy paste and use code tags it will work.
    Do you still have a problem with this? Did you read post #7?

  12. The Following User Says Thank You to jps For This Useful Post:

    Bayonetwork (April 27th, 2013)

  13. #12
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by jps View Post
    If you want to print a new line on the end of each row you will have to determine when you have printed the last element of that row, and then print a new line. What part are you having problems with?
    This sounds like what I want to do. How do I determine when the last element is printed?

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

    Default Re: Error displaying matrix?

    All the brackets and statements are where they should be
    I don't see any brackets with the for statements which is one place they should be.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Bayonetwork (April 27th, 2013)

  16. #14
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    I don't see any brackets with the for statements which is one place they should be.
    Solved:

    public void display() {
    		for (int i = 0; i < rows(); i++){
                for (int j = 0; j < columns(); j++){
                	System.out.print(matrix[i][j] + "   ");
                	}
    			System.out.print("\n");
    			}
    		}

    Thanks for help it was the brackets I forgot. Formatting is off here but its fine in Eclipse.

    Also, I have a question about a part later in my code which I havent posted, should I do it here or start a new thread?

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

    Default Re: Error displaying matrix?

    If its in the same program, continue here.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    I do have another error checking question in the following code between the error checking start and end segment:

    import java.util.Scanner;
     
    public class Recursion {
     
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		int n = 0; String input;
     
    		System.out.print("What is our starting range: ");
    		input = keyboard.nextLine();
    		int start = Integer.valueOf(input);
    		System.out.print("What is our ending range: ");
    		input = keyboard.nextLine();
    		int end = Integer.valueOf(input);
     
    		System.out.printf("\nIterative range summation: %d\n", rangeSummationI(start, end));
    		System.out.printf("Recursive range summation: %d\n", rangeSummationR(start, end));
     
    		// error check start --------------------------------------------
     
    		System.out.print("\nHow many disks should we use for Hanoi problem: ");
    		input = keyboard.nextLine();
    		n = Integer.valueOf(input);
    		int userinput = keyboard.nextInt();
    		boolean hasInt = false;  
                    while (!hasInt){
    			if (userinput < 0)
    				System.out.println("Invalid! Input must be greater than 0.");
    			if (userinput == int)
    			System.out.println("Exception: Invalid input.");
    			userinput = keyboard.nextInt();
    		}
     
    		// error checking end -------------------------------------------
     
    		System.out.println("\nSolution: ");
    		hanoiMoves(n , 'L', 'R', 'M');
    		keyboard.close();	
    	}

    When the user inputs a number of disks, I need to make sure I catch any errors (such as if the user typed in a non- integer character) and also validate that they entered an integer greater than 0.
    So I need to loop asking for input until a valid response is given. No input given for the number of disks should crash or terminate your program.
    I do not need to error check the starting range or ending range.
    So obviously I cant say if (userinput == int). But how do I check to make sure they enter an int and not something else?

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

    Default Re: Error displaying matrix?

    if the user typed in a non- integer character
    Have you tried executing the code and entering non-integer data? What happened?

    Hint: read the API doc for the nextInt() method.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #18
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    Have you tried executing the code and entering non-integer data? What happened?

    Hint: read the API doc for the nextInt() method.
    It just gives me an invalid expression error for the if (userinput == int) line. I just wrote that as a placeholder until I know to do for "int."

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

    Default Re: Error displaying matrix?

    Comment out the invalid code, compile and execute the program and enter some invalid data to see what happens.
    	int userinput = keyboard.nextInt();
            boolean hasInt = false;  
            while (!hasInt){
                ...	
                if (userinput == int) //???????WHAT FOR????
    The variable userinput is defined as an int. I don't understand what the code can be trying to test.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    Comment out the invalid code, compile and execute the program and enter some invalid data to see what happens.
    	int userinput = keyboard.nextInt();
            boolean hasInt = false;  
            while (!hasInt){
                ...	
                if (userinput == int) //???????WHAT FOR????
    The variable userinput is defined as an int. I don't understand what the code can be trying to test.
    Again, I just wrote that as a placeholder. "int" in the line if (user input == int) is a syntax error and will not compile. That is just what I want it to do but dont know how to code for it. In other words I want to loop to make sure the user enters an integer and give an error message until they do.

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

    Default Re: Error displaying matrix?

    That is just what I want it to do but dont know how to code for it
    The test does NOT make sense. userinput is an int variable and can ONLY hold int values.

    Have you removed that invalid and senseless statement, compiled the code, executed it and tried to enter invalid data to see what happens?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    The test does NOT make sense. userinput is an int variable and can ONLY hold int values.

    Have you removed that invalid and senseless statement, compiled the code, executed it and tried to enter invalid data to see what happens?
    Yes I deleted that line and if I input 0 it does nothing and if I input a character it throws an exception on my int userinput = keyboard.nextInt(); for an input mismatch. Obviously because I enter a char when it can only hold an int.

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

    Default Re: Error displaying matrix?

    The current code will not all you to enter invalid data. Is that what you want it to do?

    Do you know how to code a try{}catch block to catch the error?
    If you don't understand my answer, don't ignore it, ask a question.

  26. #24
    Junior Member
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error displaying matrix?

    Quote Originally Posted by Norm View Post
    The current code will not all you to enter invalid data. Is that what you want it to do?

    Do you know how to code a try{}catch block to catch the error?
    No I dont know how to catch the error. I just want to create a while loop that prints a message saying Error invalid entry until they input a valid integer.

    Example:

    How many disks should we use for Hanoi problem: -4
    Invalid! Input must be greater than 0.

    How many disks should we use for Hanoi problem: 7 dfgdfg dgdfgd
    EXCEPTION: Invalid input.

    How many disks should we use for Hanoi problem: sdfdfsdf
    EXCEPTION: Invalid input.

    How many disks should we use for Hanoi problem: 0
    Invalid! Input must be greater than 0.

Similar Threads

  1. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  2. Matrix Generator
    By kakb9091 in forum What's Wrong With My Code?
    Replies: 29
    Last Post: May 10th, 2012, 12:00 PM
  3. Help with Identity matrix!
    By javabeginner123456 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 9th, 2011, 06:42 PM
  4. Adding two matrix together
    By papated21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 01:52 PM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM