Good morning to all:zzz: I am a beginner programmer in Java, and I am having an issue I don't quite understand for a program I am working on. I am hoping that someone here with some more experience than myself could take a look at my code and see what I am doing incorrectly; I am a beginner so some of this stuff might seem silly to most, please don't flame, I hate asking for help, but I enjoy progamming immensley and need a bit of help. If anyone could assit me I would be very greatfull.

The issue I am having is that I canno't get the program to spit out the total number of scores entered...it just keeps tabbing back to the beginning of the line of the first int. the user enters....here is my code.

/* Micheal W. Smith
 * CMP-2564-A
 * Java Programming
 *
 * Program Name:        Exam Grades
 *
 * Program Purpose:     Write a fully documented Java program tha aks the user to enter a list of
 *                      exam scores given as integer percentages in the range of 0 to 100. The user
 *                      should indicate at the end of the list by entering a sentilen value of -1.
 *                      The program should indlude a loop for inputting the values. Within the loop, the
 *                      program should count the total number of scores entered and output it to the screen. 
 */


import java.util.Scanner; //allows keyboard to accept input


public class ExamGrades
{
  
    public static void main(String[] args)
    {
         //accepts input from keyboard
        Scanner kbd = new Scanner(System.in);

        //variables
        int examScores, count = 0;
        double nextScore;

        

        System.out.print("Enter a series of test grades seperated by a space: ");
        examScores = kbd.nextInt();

        while (examScores != -1)
        {
            nextScore = kbd.nextDouble();
            nextScore = examScores++;
            
        }

        System.out.print("You entered " + count + " total scores");
        
        

    }

Again I would greatly appricate it if someone could take a peek and give me some direction / advice / example.

Thank You

Recommended Answers

All 2 Replies

Line 38 you increment examScores, but line 42 you print count.
I don't understand what line 38 is supposed to be doing anyway, but incrementing count here would be as good idea

What the poster above said is the problem, you're printing the count variable (which is always 0 in this program), and you have no actual variable to store the count inputs, the code between line 35 and 40 doesn't do what you think it does either.

Something I learned being a student in many programming classes, a LOT of professors consider what you did on line 27 sloppy, initializing variables on a single line declaration; if your professor is anal about things, it might lose you points.

In line 32, you don't say that -1 is the cancel input for the sentinel loop (that's the name used for loops that require a specific input to exit like this one), and the line is misleading as well, a better line would be:

System.out.println("Enter a series of test grades one at a time, pressing enter after each grade is entered. Input \"-1\" to end")

As for the problem with the program in regards to lines 35-40:

Inside of the loop, assuming the banner comment you made describes the ENTIRE problem you have to do, you have it half right, since you don't need to do any math on the scores you input, this line WOULD be correct, if not for the fact that the way you wrote the loop makes it so that it would be an infinite loop. The loop will work if you replace this line(the 1st of the loop):

nextScore = kbd.nextDouble();

with

examscores = kbd.nextDouble();

This is because the value that ends the loop is examScores, not nextScore, and since there is no way to change examScores in your loop, making it infinite.


The 2nd line of the loop is also a problem, you're incrementing the input value, you SHOULD be incrementing the count variable, so the 2nd line of the loop should be this:

count++;

++ in java is shorthand for writing variable = variable + 1, therefore the line you had before is confusing since.


Edit: I wrote up a quick example of something that would work for your situation (I made a general example since I didn't want to do your homework for you)

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner keyIn = new Scanner(System.in);
        int sentinelValue = 0;
        int count = 0;

        System.out.println("Enter a series of values, ending with \"-1\"");
        sentinelValue = keyIn.nextInt();
        while(sentinelValue != -1)
        {
            count++;
            sentinelValue = keyIn.nextInt();
        }

        System.out.println("Number of values entered: " + count);
    }
}

I make the scanner and two variables, both initialized to 0, output a line explain the program, then take the sentinel value once BEFORE entering the loop, therefore if the user enters -1 as the first entry, it will exit without incrementing the counter. A problem with this is that the first value won't increment the count ever, however, the -1 that is used to end the loop gets added to the count, negating this problem.

Inside the loop, the counter is incremented, and the sentinel value is captured again to check the value of the loop condition. When -1 is input, the loop breaks and the last out line is hit, with the correct value, hopefully.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.