-2

Complete question is: Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. What I attempted thou it has errors:

public class SquareMatrix {
  public static void main(String[] args) {
  int sqr[][]= {{1,3,5},{2,4,6}};
  System.out.println("Your Original Matrix: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(sqr[i][j] + " ");
  }
  System.out.println();
  }
  Square();
  ShowNumbers();
  }
static void Square(){
    
  for(int i = 0; i <= 1; i++) {
  for(int j = 0; j <= 2; j++) {
  sqr[i][j] = sqr[i][j] * sqr[i][j];
  } 
  }
  }
  static void ShowNumbers(){
      
  System.out.println("Matrix after changes: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(sqr[i][j] + " ");   
  }
  System.out.println();
  }
} 
}

Also how would I write it if I wanted an input from the user for the col and row for a specific range of 0 to 10, I made an alternative version with many errors below. Below code is not as important as the one above

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
     
     int row, col, i, j;
       int arr[][] = new int[10][10];
       Scanner scan = new Scanner(System.in);
      try{
       System.out.print("Enter Number of Row for Array (max 10) : ");
       row = scan.nextInt();
       System.out.print("Enter Number of Column for Array (max 10) : ");
       col = scan.nextInt();
       if(0>=row && row<=10 || 0>=col && col<=10 ){
           }
      }
       catch (Exception e){
           System.out.println("(!) Wrong input...\n");
       }
       
       System.out.print("Enter " +(row*col)+ " Array Elements : ");
       for(i=0; i<row; i++){
           for(j=0; j<col; j++){
               arr[i][j] = scan.nextInt();
           }
       }
    }
       static void square(){
        arr[row][col] = arr[row][col] * arr[row][col];
       }
       
       static void ShowNumbers(){
       System.out.print("The Array is :\n");
       for(i=0; i<row; i++)
       {
           for(j=0; j<col; j++)
           {
               System.out.print(arr[i][j]+ "  ");
           }
           System.out.println();
       }}}

PS I am new to java and would appreciate a response with the whole code pasted not just a section of the code so I don't get lost or with the number of the line the error is from. thanks for any help

  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular How do [I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." even if it is no homework. Please add the **complete error message* – Timothy Truckle Jun 21 '21 at 20:43
  • I down voted because ["It's not working" is not helpful](http://idownvotedbecau.se/itsnotworking/) – Timothy Truckle Jun 21 '21 at 20:45
  • I don't understand what you talking about. If you really want to know what is not working on the first lines of code, the method Square is not recognizing the variables, and the second one I already mentioned in the question – Hussain AlAdraj Jun 21 '21 at 20:51
  • @HussainAlAdraj Fix the indentation of your source code. This made it easier for everyone (specially you) to read and understand the code. – Progman Jun 21 '21 at 20:55
  • @HussainAlAdraj You can provide the values as parameters to your methods. That way you can call these methods like `Square(sqr);` – Progman Jun 21 '21 at 20:59
  • @progman I Got errors: SquareMatrix.java:14: error: expected static void Square(sqr){ – Hussain AlAdraj Jun 21 '21 at 21:01
  • @HussainAlAdraj Check https://stackoverflow.com/questions/31625043/how-to-use-parameters-and-arguments-in-java on how to define parameters. – Progman Jun 21 '21 at 21:03

1 Answers1

1

Since you are starting from scratch, it would be good to go over the basics of the language again. Take a look at oracle classvars For your upper problem you need to understand the difference between local and instance variables and the difference between static and non static variables respectively. To solve your issue just move the declaration of your array out of the main method and add a static modifier:

public class SquareMatrix {
    static int sqr[][] = {{1, 3, 5}, {2, 4, 6}};

    public static void main(String[] args) {

    //rest of your code
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28