HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Distinguishing user input numbers and characters

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
distinguishingusernumbersinputcharactersand

Problem

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"+"Plea

Solution

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."
        );
    }
}

Code Snippets

Marktotal= Marktotal + Mark;
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."
        );
    }
}

Context

StackExchange Code Review Q#82525, answer score: 4

Revisions (0)

No revisions yet.