2

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.

4
  • 1
    Make both the arrays global. Then you dont need to pass them in all functions. Commented Nov 5, 2013 at 12:24
  • How I can make them global? Thanks Commented Nov 5, 2013 at 12:37
  • 1
    declare them outside main function. Then any function can access them. Commented Nov 5, 2013 at 12:53
  • And you dont need 2 scanners - exam and coursework. As YOU ARE USING ONLY ONE STREAM, YOU CAN USE ONLY ONE SCANNER for the stream (system.in). Commented Nov 5, 2013 at 13:09

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

add the call to the method after:

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.