5
\$\begingroup\$

I am currently taking a computer science class in high school, and it is my first time programming (2 weeks since I started). I have currently finished my program; however, there were a few details that bothered me. I was supposed to create a program which can calculate average of individual marks and then calculate all of the average marks previously entered by user.

However, I noticed something that bothered me, which was the fact that if I were to enter a number when asked to enter a name, it would assume that number is a name. Also, when I asked for the 5 marks, the program would run fine until an error occurs if I accidentally type in a letter or word. I have done some research and found system scanners; however, I still don't understand the concept in a looping situation.

import java.io.*;
public class loopingEx6Final {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        double average, totalaverage = 0;
        int Mark, Marktotal=0,  marktotalsum=0, count=0;
        String strName, strMark;
        System.out.println("This program will calculate an individual's personal average and then calculate the class average whn instructed.");
        System.out.println("Please type (finish) in order to calculate class average.");
        System.out.println("Please enter a name.");
        strName = br.readLine ();

        while (!strName.equals("finish")){
            System.out.println("The follwing will calculate the average five marks of "+ strName +".");
            System.out.println("Please enter 5 marks.");
            count++;
            for  (int i=0;i<5;i++) {
                Mark = Integer.parseInt(br.readLine());
                    Marktotal= Marktotal + Mark;        

            }
            average = Marktotal/5;
            System.out.println(strName+"'s average is "+average);
            Marktotal= 0;
            System.out.println("\n"+"Please enter a name.");
            strName = br.readLine ();
            totalaverage=(totalaverage+average)/count;
        }       
        System.out.println("The class average of all input grades is:");
        System.out.println(totalaverage);
        System.out.println();
        System.out.println("Thank you for using the program.");
    }
}
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! You'll probably go into error handling, "Exceptions" later on in your class. Though if you're interested there are ways you can use a while loop to make sure that the input is correct, though I imagine what you have is sufficient. For the future though, errors, bugs, asking for unwritten code are off topic for Code Review. \$\endgroup\$ – Legato Feb 25 '15 at 2:37
4
\$\begingroup\$

Variables in java are camelCase, it's odd that you follow it in cases like strName but ignore it with MarkTotal or totalaverage. No matter what style you choose, be consistent about it, for instance I like here how the latter variable has an equals sign between it and the 0, yet everything's together in the proceeding line. Try to maintain a space between your variables, and declare only 1 variable per line. Additionally, make it a habit to define variables near where they're actually used. Narrow your scope as much as possible as it will help in future maintainability.

These may seem like nitpicks for now, but down the line and as your programs grow they will make things easier to locate, read, and understand.

You don't need to repeatedly call System.out.println, you can use \n to add a new line where necessary. Your several calls in the beginning can be something like this:

This line:

 Marktotal= Marktotal + Mark;

Can be shortened to MarkTotal += Mark; but minding the tips I said earlier it would be more markSum += mark;

Lastly you have some variables that are only used once, when that's the case you can usually get rid of them and simply replace them with the method call.

Here's your implementation with these suggestions:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ForNing {
    public static void main(String args[]) throws IOException {
        BufferedReader input = new BufferedReader(
            new InputStreamReader(System.in)
        );

        System.out.print(
            "This program will calculate an individual's personal average" +
            " and then calculate the class average when instructed. " +
            "\nEnter a name: "
        );

        String studentName = input.readLine();
        int studentCount = 0;
        double classAverage = 0;

        while (!studentName.equals("finish")){
            System.out.println(
                "The follwing will calculate the average five marks of "+
                studentName +".\nPlease enter 5 marks."
            );

            studentCount++;

            int markSum = 0;
            for (int i = 1; i <= 5; i++) {
                markSum += Integer.parseInt(input.readLine());      
            }

            double average = markSum / 5;
            markSum = 0;
            classAverage += average;

            System.out.println(
                studentName + "'s average is: " + average + 
                ".\n\nPlease enter the next student's name " +
                "or enter 'finish' to calculate the class average."
            );

            studentName = input.readLine();
        }

        classAverage /= studentCount;

        System.out.println(
            "The class average of all input grades is: " + classAverage +
            ".\n\nThank you for using the program."
        );
    }
}
\$\endgroup\$
3
\$\begingroup\$

Standards

  • class names should follow PascalCase
  • variable names should be camelCase always, and always start with a lower-case character (Mark -> mark, marktotalsum -> markTotalSum). Following this will increase the readability of your code.
  • your spacing is off. Adding spaces around = or * and after ; for example will increase readability. Use any IDE to manage this for you.
  • don't declare multiple variables on one line, it makes it easy to overlook some.

Misc

  • extract code to different methods (such as printHelp, getInput, printResult, calculateAverage), to make your code reusable and easier to read. It also has the benefit that you can write automated tests for each method.
  • don't import *, because it doesn't tell a reader what exactly you imported.
  • declare variables where they are needed, not at the top of your method (this increases readability, as readers don't have to remember variables that are not actually relevant at that time).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.