0
import java.util.Scanner;

public class GradePointAverage {

    public static void main(String[] args) {
        Scanner peace = new Scanner(System.in);
        System.out.print("How many subjects do you want to enter?: ");

        int a=peace.nextInt();
        String[] b = new String[a];

        for(int i=0;i<a;i++) {
            b[i]="";    
            System.out.print("Enter Subject No "+(i+1)+" ");
            String c=peace.next();
        }

        for(i=0;i<b.length;i++) {
            System.out.print(b[i]);
        }
    }
}

Greetings. :) We have a programming experiment and well I was stuck in this part. I need to ask the user how many subjects he wants to enter and ask the user to input the subjects. I think I already entered the subjects on the array but when i want to see the content of the array it won't give me my desired output, the subjects i entered won't appear. Please help, I'm new here on this site and it's my first time to ask a question on a forum like this. Hoping that someone would reply. Thanks.

3
  • replace String c = peace.next(); with b[i] = peace.nextLine(); Commented Mar 10, 2015 at 8:48
  • Jens is correct, you do nothing with String c. Also you might want to use more appropriate variable names. It will help prevent confusion. ie: Scanner peaceScanner, int a -> int subjectCount. int i is conventional though so no problem there,although I would declare it in your loop -> for(int i... Commented Mar 10, 2015 at 8:51
  • 1
    Just a suggestion: name your variables with meaningful names! Not a, b, c, but subjectsNumber, subjects, subjectsName. Commented Mar 10, 2015 at 8:52

3 Answers 3

4

You never put the subject into the array.

import java.util.Scanner;

public class GradePointAverage
{

    public static void main(String[] args)
    {
        int i;
        Scanner peace=new Scanner(System.in);
        System.out.print("How many subjects do you want to enter?: ");
        int a=peace.nextInt();
        String []b=new String [a];
        for(i=0;i<a;i++)
        {
            System.out.print("Enter Subject No "+(i+1)+" ");
            b[i]=peace.next();

        }   
            for(i=0;i<b.length;i++)
            {
                System.out.print(b[i]);
            }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You never assign the Strings to the array.

Change

String c=peace.next();

to

b[i] = peace.next();

In addition, you should probably add some separator (or new line) when printing the array :

public static void main(String[] args) 
{
    Scanner peace = new Scanner(System.in);
    System.out.print("How many subjects do you want to enter?: ");
    int a = peace.nextInt();
    String[] b = new String[a];
    for(int i = 0; i < a; i++) {
        System.out.print("Enter Subject No " + (i + 1) + " ");
        b[i] = peace.next();
    }
    for(i = 0; i < b.length; i++) {
        System.out.print(b[i] + " ");
    }
}

1 Comment

Oh gosh. Silly me. Haha. Thank you. :)
0
import java.util.Scanner;
public class GradePointAverage
{
    public static double processAverage(int []SubjectGrades,int SubjectsNumber)
    {
        double sum=0;
        double Ave=0;
        for(int i=0;i<SubjectGrades.length;i++)
        {
            sum=SubjectGrades[i]+sum;
        }
            Ave=sum/SubjectsNumber;
            return Ave;
    }
    public static int processNumericalValue(double Ave)
    {
        int Numeral;
        if(Ave>=98.0&&Ave<100.0)
        {
            Numeral=4;
        }
        else if(Ave>=90.0&&Ave<98.0)
        {
            Numeral=3;
        }
        else if(Ave>=80.0&&Ave<90.0)
        {
            Numeral=2;
        }
        else if(Ave>=75.0&&Ave<80.0)
        {
            Numeral=1;
        }
        else
        {
            Numeral=0;
        }
            return Numeral;
    }
    public static void processLetterGrade(int Numeral)
    {
        if(Numeral==4)
        {
            System.out.println("Congratulations!");
        }
        else if(Numeral==3)
        {
            System.out.println("Your Letter Grade is B!");
        }
        else if(Numeral==2)
        {
            System.out.println("Your Letter Grade is C!");
        }
        else if(Numeral==1)
        {
            System.out.println("Your Letter Grade is D!");
        }
        else
        {
            System.out.println("You Failed!");
        }
    }
    public static void main(String[] args)
    {
        double Ave=0;
        Scanner peace=new Scanner(System.in);
        System.out.print("How many subjects do you want to enter?: ");
        int SubjectsNumber=peace.nextInt();
        int []SubjectGrades=new int [SubjectsNumber];
        String []Subjects=new String [SubjectsNumber];
        for(int i=0;i<SubjectsNumber;i++)
        {
            Subjects[i]=""; 
            System.out.print("Enter Subject No "+(i+1)+": ");
            Subjects[i]=peace.next();
            System.out.println("What is your grade in "+Subjects[i]+": ");
            SubjectGrades[i]=peace.nextInt();
        }
            int Numeral;
            Ave=processAverage(SubjectGrades,SubjectsNumber);
            System.out.println("Your General Average is: "+Ave);
            Numeral=processNumericalValue(Ave);
            System.out.println("Numerical Value is: "+Numeral);
            processLetterGrade(Numeral);
    }
}

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.