user2956231
user2956231

Reputation: 27

How to use array from one method in another?

I find problem to use array form one method in another method.

In first method I take input from user and saved data to two arrays.

In second method I have to shows these information from array.

import java.util.Scanner;

public class MarkCalculator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        computeMark();
        computeResult();
    }

    public static void computeMark ()
    {

        Scanner exam = new Scanner (System.in);
        Scanner coursework = new Scanner (System.in);

        int num = 12;
        int[] exam_grade = new int[num];
        int[] coursework_grade = new int[num];

        for (int i=0+1; i<3; i++){

           System.out.printf(i+". Modelue"+" Enter grade of exam:");
           exam_grade[i]=exam.nextInt();

           System.out.printf(i+". Modelue"+" Enter grade of coursework:");
           coursework_grade[i]=coursework.nextInt();   
        }

        System.out.println();   
        System.out.println();
        System.out.println("Your grades are: ");


    }


    public static void computeResult (int[] coursework_grade, int[] exam_grade)
    {

        for (int i = 0+1; i<3; i++) {

            System.out.println(i+". Module: "+ "Exam: "+exam_grade[i]+" Coursework: "+coursework_grade[i]);

        }

    }



}

The problem is, I have no idea how to pass information from array to the second method. I couldn't find any solution. Can someone help me pls?

Thanks.

Upvotes: 2

Views: 7763

Answers (4)

Apoorv Gunjan Pathak
Apoorv Gunjan Pathak

Reputation: 65

You can have access to the array using these two methods:

  1. Make the array global.
  2. Declare the array in the main class and pass them in an individual method.

Code declaring the array in the main class and passing it in the individual method:

import java.util.Scanner;

public class MarkCalculator {

public static void main(String[] args) {
    int[] exam_grade = new int[12];
    int[] coursework_grade = new int[12];
    computeMark(coursework_grade,exam_grade);
    computeResult(coursework_grade,exam_grade);
}

public static void computeMark (int[] coursework_grade, int[] exam_grade)
{
    Scanner input = new Scanner (System.in);
    for (int i=0+1; i<3; i++){
       System.out.printf(i+". Modelue"+" Enter grade of exam:");
       exam_grade[i]=input.nextInt();

       System.out.printf(i+". Modelue"+" Enter grade of coursework:");
       coursework_grade[i]=input.nextInt();   
    }
    System.out.println();   
    System.out.println();
    System.out.println("Your grades are: ");
}

public static void computeResult (int[] coursework_grade, int[] exam_grade)
{

    for (int i = 0+1; i<3; i++) {
       System.out.println(i+". Module: "+ "Exam: "+exam_grade[i]+" Coursework: "+coursework_grade[i]);
    }
  }
}

Code declaring the array in the global area:

import java.util.Scanner;

public class MarkCalculator {
static int[] exam_grade = new int[12];
static int[] coursework_grade = new int[12];
public static void main(String[] args) {
    computeMark();
    computeResult();
}

public static void computeMark ()
{
    Scanner input = new Scanner (System.in);
    for (int i=0+1; i<3; i++){
       System.out.printf(i+". Modelue"+" Enter grade of exam:");
       exam_grade[i]=input.nextInt();

       System.out.printf(i+". Modelue"+" Enter grade of coursework:");
       coursework_grade[i]=input.nextInt();   
    }
    System.out.println();   
    System.out.println();
    System.out.println("Your grades are: ");
}

public static void computeResult ()
{
    for (int i = 0+1; i<3; i++) {
        System.out.println(i+". Module: "+ "Exam: "+exam_grade[i]+" Coursework: "+coursework_grade[i]);
    }
  }
}

Suggestion:

  1. No need to use two scanner variables.
  2. No need to use the num variable, directly declare the size of the array in it. Unnecessary increasing the variable count makes no sense.

Upvotes: 0

user4919188
user4919188

Reputation:

make the arrays, global. out them outside the public static void main(String[] args) and it will be global.

Upvotes: 0

Thirumalai Parthasarathi
Thirumalai Parthasarathi

Reputation: 4671

this is all it takes.

computeResult(coursework_grade,exam_grade);

you can pass an array or any object to a method as you would do with a primitive type. But remember when passing an object if the object's state is changed in the called method it gets reflected in the object itself and all references of the object would contain the affected object.

Upvotes: 1

RamonBoza
RamonBoza

Reputation: 9038

add the call to the method after:

System.out.println("Your grades are: ");
computeResult(coursework_grade,exam_grade);

Upvotes: 0

Related Questions