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 8289599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:32:37+00:00 2026-06-08T12:32:37+00:00

I am doing java programming practice from a website. I have to display this

  • 0

I am doing java programming practice from a website. I have to display this out put using arrays.

Enter the number of students: 3
Enter the grade for student 1: 55
Enter the grade for student 2: 108
Invalid grade, try again...
Enter the grade for student 2: 56
Enter the grade for student 3: 57
The average is 56.0

This is my source code so far. It is showing results but not working properly. It takes number of students & grade for 1st student. But then it halts, if I press any number & press enter, it asks for next grade & so on. And this way it prints average at the end. If I enter not allowed input, it catches that.
What is wrong in this code.

package array;

import java.util.Scanner;

public class GradesAverage {

    public static void main(String[] args) {

        int numStudents, sum=0;
        int[] grades;

        Scanner in = new Scanner(System.in);

        System.out.print("Enter number of students : ");
        numStudents = in.nextInt();

        grades = new int[numStudents];
      //System.out.println(grades.length);

        for (int i = 0; i < numStudents; i++) {
              System.out.print("Enter grade of student "+i+" :");
              grades[i] = Integer.parseInt(in.next());
              if(grades[i]>100 || grades[i]<0)
                {
                  System.out.println("It is not a valid number!");
                  i--;
                  continue;
                }

              //grades[i] = Integer.parseInt(in.next());
              sum+=grades[i];
        }

        System.out.print("Average grades are: "+sum/numStudents);

        in.close();
    }

}
  • 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-08T12:32:40+00:00Added an answer on June 8, 2026 at 12:32 pm

    Instead of using your temp variable, create a variable like this:

    int grade = in.nextInt()

    Then check if(grade > 100 || grade < 0) // error message

    Don’t use in.next() twice, because it then asks the user for another number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing java programming practice from a website. I have to display this
I'm trying out several exercises from a Java programming book. I have the code
I've been doing object-oriented programming for most of the past 7 years, using Java
I'm doing this programming exercise out of a textbook where we are given an
I'm used to doing Java programming, where you never really have to think about
I am programming in Java doing an android app, in my manifest I have
I am doing some basic Java programming where i have to create classes which
Right now, I am doing this EJB tutorial: http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/#heading_toc_j_23 , but instead of GlassFish
I'm doing a Java Record/Replay tool and I need to launch Java applications from
I'm doing some JAVA coding at home and at work. At home i have

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.