Hi, is there any way to return the array from the Checkgpa class back into the Gpa class for an output? I am still new in this topic.

public class Gpa {
	private String name;
	private char result;
	private double gradep;
	private int num, sem, ch;
	public Gpa (String na, char r, double gp, int n, int s, int c) {
		setGpa (na,r,gp,n,s,c);
	public void setGpa (String na, char r, double gp, int n, int s, int c) {
		name = na;
		result = r;
		gradep = gp;
		num = n;
		sem = s;
		ch = c;
	public String getName() {
		return name;
	public char getResult() {
		return result;
	public double getGradep() {
		return gradep;
	public int getNum() {
		return num;
	public int getSem() {
		return sem;
	public int getCh() {
		return ch;
	public int countTch() {
		int tch=0;
		tch += ch; 
		return tch;
	public double countGpa() {
		double gpa = ch*gradep/countTch();
		return gpa;
	public String display() {
		String s = "Student name: "+name+"\n";
		s += "Student number: "+num+"\n";
		s += "Semester: "+sem+"\n";
		s += "Result: "+result+"\n";
		s += "Total credit hour: "+countTch()+"\n";
		s += "GPA: "+countGpa()+"\n";
		s += "\n\n";
		return s;

import java.util.Scanner;

public class Checkgpa {
	public static void main (String args[]) {
		Scanner sc = new Scanner (System.in);
		char result[] = new char[5];
		int ch[] = new int[5];
		String name;
		int num, sem;
		double gp;
		System.out.print("Enter student name: ");
		name = sc.nextLine();
		System.out.print("Enter student number: ");
		num = Integer.parseInt(sc.nextLine());
		System.out.print("Enter current semester: ");
		sem = Integer.parseInt(sc.nextLine());

		for (int i=0; i<5; ++i)
			System.out.print ("Enter result for subject"+(i+1)+" D,M,P,F");
			result[i] = sc.next().toUpperCase().charAt(0);
			if (result[i] == 'D')
				gp = 4;
			else if (result[i] == 'M')
				gp = 3;
			else if (result[i] == 'P')
				gp = 2;
			else if (result[i] == 'F')
				gp = 0;		
			System.out.print ("Enter credit hour for subject"+(i+1)+" 2,3,4");
			ch[i] = Integer.parseInt(sc.nextLine());
		Gpa g = new Gpa();
        g.setGpa (name, result[], num, sem, ch[]);
        String out = g.display();
        System.out.print (out);

yes, you can create a method in your GPA class which take the array as parameter and then print it, but that is very bad OO design.

i currently have a problem at this part. when i enter any of those character the program crashes. how do i solve it?

for (int i=0; i<5; ++i)
            System.out.print ("Enter result for subject "+(i+1)+" (D,M,P,F): ");
            result = sc.next().toUpperCase().charAt(0);
            if (result == 'D')
                gp[i] = 4;
            else if (result == 'M')
                gp[i] = 3;
            else if (result == 'P')
                gp[i] = 2;
            else if (result == 'F')
                gp[i] = 0;

ok, what is 'result'?
in your first code it was an array, and now you're trying to compare it as being a single char.

if it's still an array, that means it's actually an object which you are treating as a primitive (a single char) then there's your problem.

This article has been dead for over six months. Start a new discussion instead.