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

Thread: matrix task in java

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default matrix task in java

    Hello.

    I have a inverse matrix task.Teacher said us that we have to create little a program which is finds inverse of the matrix which is given in code.
    I did something like this and when I gave him this task he said me that "I want the program calculate itself matrix size.You wont give size of matrix like int[4][4]; this.You just will write the matrix in your code and the program will change as depend to size and calculate automatically as the matrix."

    I some more explain for all of you understand more.
    Example I write in code
    3x3 a matrix okay.Below an example.

    3 4 6
    1 5 7
    1 3 7

    This is 3x3 matrix so its calculating.And I want now 4x4 matrix.

    4 5 1 6
    1 4 5 6
    2 4 5 5
    1 2 4 7

    Also this matrix will calculate as automatically depend to matrix size.

    I musnt write [4][4] or [3][3].Instead this I need to write something like will change as matrix size automatically.I hope you understand.Because Im really bored with this.Its just second month in programming for us and I have no idea about this task anymore.

    Please help me.


    import java.util.Scanner;
    public class inversematrix {
    public static void main(String[] args)
    {
    int[][] matrix = new int[4][4];
    Scanner inputUser = new Scanner(System.in);
    System.out.println("Write matrix number");
    for(int row=0; row<4; row++)
    {
    for(int column=0; column<4; column++)
    {
    System.out.print("A"+(row+1)+(column+1)+" = ");
    matrix[row][column] = inputUser.nextInt();
    }
    }
     
    System.out.println("4 on 4 matris");
    for(int row=0; row<3; row++)
    {
    for(int column=0; column<3; column++)
    {
    System.out.printf("%3d",matrix[row][column]);
    }
    System.out.print("\n");
    }
     
    System.out.println("Inverse of matrix calculating");
    int matrixInvers[][] = {{matrix[1][1]*matrix[2][2]-matrix[1][2]*matrix[2][1],matrix[0][2]*matrix[2][1]-matrix[0][1]*matrix[2][2],matrix[0][1]*matrix[1][2]-matrix[0][2]*matrix[1][1]},
    {matrix[1][2]*matrix[2][0]-matrix[1][0]*matrix[2][2],matrix[0][0]*matrix[2][2]-matrix[0][2]*matrix[2][0],matrix[0][2]*matrix[1][0]-matrix[0][0]*matrix[1][2]},
    {matrix[1][0]*matrix[2][1]-matrix[1][1]*matrix[2][0],matrix[0][2]*matrix[2][0]-matrix[0][0]*matrix[2][1],matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]}};
     
    System.out.println("Inverse matrix");
    for(int row=0; row<3; row++)
    {
    for(int column=0; column<3; column++)
    {
    System.out.printf("%3d",matrixInvers[row][column]);
    }
    System.out.print("\n");
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

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

    Define the size of the array using the value in a variable instead of hardcoding 3 or 4:
    int theSize = <GET a value somewhere>;
    ...
    int[] anArray = new int[theSize];


    Use the array's .length attribute to get the size of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Define the size of the array using the value in a variable instead of hardcoding 3 or 4:
    int theSize = <GET a value somewhere>;
    ...
    int[] anArray = new int[theSize];


    Use the array's .length attribute to get the size of the array.
    Okay I edited my post.So

    int theSize = <inputUser>;
    ... // this part is what I didnt get
    int[] anArray = new int[theSize];

    will be like this?

    Could you explain some more ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    Please edit the code in the post and give it proper formatting. Nested statements should be indented 3-4 spaces. All statements should NOT start in the first column.

    int[] anArray = new int[theSize]; // define int array with theSize number of elements
    Did you try putting that code in the program to see if it worked?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: matrix task in java

    Quote Originally Posted by Norm View Post
    Please edit the code in the post and give it proper formatting. Nested statements should be indented 3-4 spaces. All statements should NOT start in the first column.

    int[] anArray = new int[theSize]; // define int array with theSize number of elements
    Did you try putting that code in the program to see if it worked?
    I puted your code but I got error cause of I did something wrong I think.

    You said " define int array with thesize number of elements" but already I want the size is will be depend to that matrix which is will given.

    Or you mean like this

    import java.util.Scanner;
    public class inversematrix {
    public static void main(String[] args)
    {
    Scanner inputUser = new Scanner(System.in);
    int theSize;
    int theSize = <inputUser>;
    ...
    int[] anArray = new int[theSize];
    System.out.println("Write matrix number");

    I thank you for your helps but Im really raw in programming yet.Please dont get mad

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    I got error
    Please copy the full text of the error messages and paste it here.

    The code I posted was meant as an example. It was not supposed to be copied directly into your code.

    I want the size is will be depend to that matrix which is will given.
    In my sample code, set the variable: theSize to the value that was given.

    When posting code be sure to 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.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error messages and paste it here.

    The code I posted was meant as an example. It was not supposed to be copied directly into your code.

    In my sample code, set the variable: theSize to the value that was given.

    When posting code be sure to wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Code and explains are below.



    int theSize = <inputUser>;   
    */
    Multiple markers at this line
    	- Syntax error on token(s), misplaced 
    	 construct(s)
    	- Syntax error on token "<", delete this 
    	 token
    	- Duplicate local variable theSize
    	- Syntax error on tokens, delete these 
    	 tokens
    */
    ... 
    int[] anArray = new int[theSize];  /// anArray can not be resolved to a variable
     
    System.out.println("Write matrix number");

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    I can't tell what the full code is from what you posted. When I put these two lines in a method and compile it, there are no errors:
          int theSize = 3;
          int[] anArray = new int[theSize];

    Make a small, complete class file that compiles and shows the error and post it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    mockingbird (May 9th, 2013)

  10. #9
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Quote Originally Posted by Norm View Post
    I can't tell what the full code is from what you posted. When I put these two lines in a method and compile it, there are no errors:
          int theSize = 3;
          int[] anArray = new int[theSize];

    Make a small, complete class file that compiles and shows the error and post it.
    I begin to understand somethings there is written "anArray" is not an Array.I wrote [] too but dont know why.Code and error below.

    import java.util.Scanner;
    public class inversematrix{
    public static void main(String[] args)
    {
    Scanner inputUser = new Scanner(System.in);
    int theSize = 3;
    int[] anArray = new int[theSize];
    System.out.println("Write matrix number");
       for(int row=0; row<4; row++)
    {
       for(int column=0; column<4; column++)
    {
       System.out.print("A"+(row+1)+(column+1)+" = ");
    anArray[row][column] = inputUser.nextInt();  // The type of the expression must be an array type but it resolved to int

    And rest of code is like I wrote in first message.Also another parts have error too but I think if we can solve this part then I can finish rest of task by myself.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    Please edit the post and add proper indentations to the code. Nested statements should be indented 3-4 spaces.
    Post the error message in quote tags. Don't mix it in with the code which makes the code and the error message hard to read.

    The code defines anArray as a one dim array
    int[] anArray =
    but tries to use it as a two dim array.
    anArray[row][column] =

    Either define the array as two dimensional
    or only use it as a one dim array
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Okay here the full code.

    import java.util.Scanner;
    public class keke {
    public static void main(String[] args)
    {
    Scanner inputUser = new Scanner(System.in);
    int theSize = 3;
    int[] anArray = new int[theSize];
    System.out.println("Write matrix number");
       for(int row=0; row<4; row++)
    {
       for(int column=0; column<4; column++)
    {
       System.out.print("A"+(row+1)+(column+1)+" = ");
    anArray[row][column] = inputUser.nextInt();
    }
    }
     
    System.out.println("4 na 4 matris");
       for(int row=0; row<3; row++)
    {
       for(int column=0; column<3; column++)
    {
       System.out.printf("%3d",matrix[row][column]);
    }
    System.out.print("\n");
    }
     
    System.out.println("Inverse of matrix calculating");
    int [][] = {{matrix[1][1]*matrix[2][2]-matrix[1][2]*matrix[2][1],matrix[0][2]*matrix[2][1]-matrix[0][1]*matrix[2][2],matrix[0][1]*matrix[1][2]-matrix[0][2]*matrix[1][1]},
    {matrix[1][2]*matrix[2][0]-matrix[1][0]*matrix[2][2],matrix[0][0]*matrix[2][2]-matrix[0][2]*matrix[2][0],matrix[0][2]*matrix[1][0]-matrix[0][0]*matrix[1][2]},
    {matrix[1][0]*matrix[2][1]-matrix[1][1]*matrix[2][0],matrix[0][2]*matrix[2][0]-matrix[0][0]*matrix[2][1],matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]}};
     
    System.out.println("Inverse matrix");
       for(int row=0; row<3; row++)
    {
       for(int column=0; column<3; column++)
    {
       System.out.printf("%3d",matrixInvers[row][column]);
    }
    System.out.print("\n");
    }
    }
    }

    And now at line 14 error is
    Multiple markers at this line
    - Line breakpoint:keke [line: 14] - main(String[])
    - The type of the expression must be an array type but it

    At line 23 error is
    matrix cannot be resolved to a variable
    At line 29 error is
    Multiple markers at this line
    - Syntax error on token "]", VariableDeclaratorId expected after
    this token
    - matrix cannot be resolved to a variable
    - matrix cannot be resolved to a variable
    ... continues the same error matrix cannot be resolved a variable
    At line 30 error is
    Multiple markers at this line
    - matrix cannot be resolved to a
    variable
    - matrix cannot be resolved to a
    variable
    .... going on same error like on 29.error
    At line 31 the error is same with 30.

    And at 38 error is
    "matrixInvers cannot be resolved to a variable"
    I know I made you sick but Im really trying to learn myself.Our teacher doesnt come to lesson often therefore Im trying to learn by myself in internet with helps of videos,e-books and another materials.

    I really appreciate what you doing.When I will know Java well then I will help somebodies like you doing

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    You need to decide whether anArray has one dimension or two dimensions.
    The code defines it with one dimension
    but tries to use two indexes with it.

    Where should the size of the array come from? The code has the variable: theSize given the value of 3?
    Should the value be read in from the user?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Here I did like u wrote

    anArray[row][column] = inputUser.nextInt();

    But it doesnt work.

    On this 14. line there is error as
    Multiple markers at this line
    - Line breakpoint:keke [line: 14] - main(String[])
    - The type of the expression must be an array type but it
    But already above of the code I definited it as int.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    Please copy the full text of the compiler's error message and paste it here.
    The posted message has cut off what follows "but it".

    First decide this: whether anArray has one dimension or two dimensions.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Decompiler error all is this

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type of the expression must be an array type but it resolved to int
    matrix cannot be resolved to a variable
    Duplicate local variable anArray
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable
    matrix cannot be resolved to a variable

    at keke.main(keke.java:14)

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: matrix task in java

    What program creates what you have posted as error messages? It does not look like the error messages that come from the javac compiler program.

    Can you answer this question:
    Is anArray to have one dimension or two dimensions?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: matrix task in java

    Norm thank u too much problem has been solved.

Similar Threads

  1. pay task: java application.
    By chenaz in forum Paid Java Projects
    Replies: 0
    Last Post: July 11th, 2012, 09:54 AM
  2. how to implement Task Pane in Java
    By bashasmarty in forum Member Introductions
    Replies: 1
    Last Post: May 11th, 2012, 01:42 PM
  3. Replies: 5
    Last Post: September 26th, 2011, 12:54 PM
  4. test task for Junior java
    By umami in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 10:31 AM