Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:35:49+00:00 2026-06-15T18:35:49+00:00

I am supposed to write a program that computes the average grade of the

  • 0

I am supposed to write a program that computes the average grade of the student depending on the number of subjects to be entered.

Here’s my codes but it doesn’t execute in the part of entering grade on subject1:

import javax.swing.*;

public class Arrays {
  static  int ctr = 0;
  public static void main(String[] args) {
    String inputStr = "";
    double num2process = 0.0;
    double sum = 0.0, ave = 0.0;
    double[] grade = new double[(int) num2process];

    while (true) {
      try {
        inputStr = JOptionPane.showInputDialog(
            "Enter number of subjects to enter: ");
        num2process = Double.parseDouble(inputStr);

        while (ctr < num2process) {
          grade[ctr] = getNumber();
          sum += grade[ctr];
          ctr++; 
        }
      } catch (NumberFormatException err) {
        JOptionPane.showMessageDialog(
            null,
            "There is an error on entry",
            "Error Message", JOptionPane.WARNING_MESSAGE);
        continue;
      }
      break;    
    }

    // Accumulate the output.
    String output = "";
    int ctr2 = 0;

    output = "";

    while (ctr2 > num2process) {
      output += ("Grade on subject " + (ctr2+1)
                + " is " + grade[ctr]);
      ctr2++;
    }
    ave = sum / num2process;
    output += "\nAverage is " + ave;

    // Display the output.
    JOptionPane.showMessageDialog(
        null,
        output,
        "The result is",
        JOptionPane.INFORMATION_MESSAGE);
  }

  public static int getNumber() throws NumberFormatException {
    String inputStr = "";
    int num = 0;

    try {
      inputStr = JOptionPane.showInputDialog(
          "Enter grade on subject " + (ctr+1));
      num = Integer.parseInt(inputStr);
      return num;
    } catch (NumberFormatException errorAgain) {
      throw errorAgain;
    }
  }
}

Please help me solve the error thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:35:49+00:00Added an answer on June 15, 2026 at 6:35 pm

    Your Code is throwing ArrayIndexOutOfBoundException because You are initializing

    double[] grade = new double[(int) num2process];

    before You get num2process, So as you have initialized double num2process=0.0
    so it is taking

    double[] grade = new double[0.0];

    So, You need to change it as follow (Initialize grade after taking num2process)

                String inputStr = "";
                double num2process = 0.0;
                double sum = 0.0, ave = 0.0;
    
                double[] grade ;          //Just Make an Instance of it
    
                while(true)
                {
                    try
                    {
                        inputStr = JOptionPane.showInputDialog("Enter number of subjects to enter: ");
                        num2process = Double.parseDouble(inputStr);
                        grade = new double[(int) num2process];     //Initialize it Here
                        while(ctr<num2process)
                        {
                            grade[ctr]= getNumber();
                            sum += grade[ctr];
                            ctr++; 
                        }
    
                    }
                    catch(Exception e){
                        //Your Exception Handling
                    }
                }
    

    Edit: As You Commented:

    It’s supposed to display the grades of subject 1 to subject 3. the average is fine but how can i reduce it two decimal places only

    Here’s the whole Program for You:

    import java.text.DecimalFormat;
    
    import javax.swing.*;
    
    public class ArrayWithException 
    {
            static  int ctr = 0;
            public static void main(String[] args)
            {
                String inputStr = "";
                double num2process = 0.0;
                double sum = 0.0, ave = 0.0;
                double[] grade ;
    
                while(true)
                {
                    try
                    {
                        inputStr = JOptionPane.showInputDialog("Enter number of subjects to enter: ");
                        num2process = Double.parseDouble(inputStr);
                        grade = new double[(int) num2process];
                        marks = new double[(int) num2process];
                        while(ctr<num2process)
                        {
                            grade[ctr] = getNumber();
                            sum += grade[ctr];
                            ctr++; 
                        }
    
                    }
                    catch (NumberFormatException err)
                    {
                        JOptionPane.showMessageDialog(null, "There is an error on entry",
                            "Error Message", JOptionPane.WARNING_MESSAGE);
                        continue;
                    }
                    break;      
    
                }
    
                //accumulate the output
                String output="";
                int ctr2 = 0;
    
                output = "";
    
                while(ctr2 > num2process)
                {
                    output +=("Grade on subject " +(ctr2+1) + " is "
                                    + grade[ctr]);
                    ctr2++;
    
                }
    
                ave = sum / num2process;
                DecimalFormat df=new DecimalFormat("##.##");   //Here it defines the pattern to follow
                ave=Double.parseDouble(df.format(ave));        //ave is formatted like df
                output +="\nAverage is " + ave;
                for(int i=0;i<grade.length;i++){ 
                    output+="\nGrade for Subject "+(i+1)+" is: "+grade[i];
                }
    
                // display the output
    
                JOptionPane.showMessageDialog(null,output, "The result is",
                            JOptionPane.INFORMATION_MESSAGE);
    
            }
    
            public static int getNumber() throws NumberFormatException
            {
                String inputStr="";
                int num = 0;
    
                try
                {
                    inputStr = JOptionPane.showInputDialog("Enter grade on subject " + (ctr+1));
                    num = Integer.parseInt(inputStr);
    
                    return num;
    
                }
                catch (NumberFormatException errorAgain)
                {
                    throw errorAgain;
                }
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to write a program that gets some PNG images from the
I am learning Java. I am supposed to write a program that converts all
I am supposed to write a program that should read from input numbers in
I'm supposed to write a program that does 2 + 2 = 4 and
I am supposed to write a program that asks the user for a positive
I am so confused right now. I am supposed to write a program that
Write a program that calculates Euler’s number e. To do this, first write a
I am supposed to write a program that calculates the total floor space in
I have a small program that I am supposed to write that makes a
I am supposed to write a program in JavaScript to find all the anagrams

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.