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

  • Home
  • SEARCH
  • 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 9220967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:28:19+00:00 2026-06-18T03:28:19+00:00

I’m writing a program that takes user input of five students and four test

  • 0

I’m writing a program that takes user input of five students and four test scores per student and puts them into arrays. It then averages the student’s scores and gives a letter grade. The program also needs an input validation that doesn’t accept scores under 0 and over 100.

So far my program validates the user and tells them that they’ve input an invalid number, but I would also like for it to prompt the user to start over after they’ve input an invalid number. I’ve tried a couple of different options as you will see below (in the enterData method), but none of which force the user to start over. Any suggestions?

import java.util.Scanner;

public class GradeBook {
    private String[] names = new String[5];

    private char[] grades = new char[5];

    private double[] scores1 = new double[5];
    private double[] scores2 = new double[5];
    private double[] scores3 = new double[5];
    private double[] scores4 = new double[5];
    private double[] scores5 = new double[5];

    int studentData = 0;
    int studentCount = 0;

    Scanner keyboard = new Scanner(System.in);

    public void enterData() {
        do {
            System.out.println("Enter the student's name:");
            String student = keyboard.nextLine();

            System.out.println("Enter the student's first test score");
            double score1 = Double.parseDouble(keyboard.nextLine());
            if (score1 > 0 || score1 < 100) {
            } else {
                System.out.println("Invalid number");
                continue;
            }


            System.out.println("Enter the student's second test score");
            double score2 = Double.parseDouble(keyboard.nextLine());
            if (score2 > 0 || score2 < 100) {
            } else {
                System.out.println("Invalid number");
                System.out.println("Please start over");
                continue;
            }

            System.out.println("Enter the student's third test score");
            double score3 = Double.parseDouble(keyboard.nextLine());
            if (score3 < 0 || score3 > 100)
                System.out.println("Invalid number");

            System.out.println("Enter the student's fourth test score");
            double score4 = Double.parseDouble(keyboard.nextLine());
            if (score4 < 0 || score4 > 100)
                System.out.println("Invalid number");

            addStudent(student, score1, score2, score3, score4);
            studentData++;
        } while (studentData < 5);
    }


    public String toString() {
        StringBuilder printout = new StringBuilder();
        for (int i = 0; i < names.length; i++) {
            if (names[i] == null)
                continue;
            printout.append("Student name: " + names[i] + "\t" + "Average test score: " +
                    getAverage(i) + "\t" + "Grade: " + grades[i] + "\n");
        }
        return printout.toString();
    }


    private void addStudent(String inName, double scr1, double scr2, double scr3, double scr4) {

        names[studentCount] = inName;
        scores1[studentCount] = scr1;
        scores2[studentCount] = scr2;
        scores3[studentCount] = scr3;
        scores4[studentCount] = scr4;
        grades[studentCount] = getLetterGrade(studentCount);
        studentCount++;
    }


    private double getAverage(int index) {
        double score1 = scores1[index];
        double score2 = scores2[index];
        double score3 = scores3[index];
        double score4 = scores4[index];
        double average = (score1 + score2 + score3 + score4) / 4.0;
        return average;
    }

    private char getLetterGrade(int index) {
        double average = getAverage(index);
        char grade;

        if (average >= 90)
            grade = 'A';
        else if (average >= 80)
            grade = 'B';
        else if (average >= 70)
            grade = 'C';
        else if (average >= 60)
            grade = 'D';
        else
            grade = 'F';


        return grade;
    }

}

I made this more difficult than needed. This works just fine.

   System.out.println("Enter the student's third test score");
    double score3 = Double.parseDouble(keyboard.nextLine());          
      while (score3 < 0 || score3 > 100)
      {
          System.out.println("Invalid score, plese reenter");
          System.out.println("Enter the student's third test score");
        score3 = Double.parseDouble(keyboard.nextLine());
      }
  • 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-18T03:28:20+00:00Added an answer on June 18, 2026 at 3:28 am
    System.out.println("Enter the student's third test score");
    double score3 = Double.parseDouble(keyboard.nextLine());          
      while (score3 < 0 || score3 > 100)
      {
          System.out.println("Invalid score, plese reenter");
          System.out.println("Enter the student's third test score");
        score3 = Double.parseDouble(keyboard.nextLine());
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.