# Distinguishing user input numbers and characters

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

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

}
average = Marktotal/5;
System.out.println(strName+"'s average is "+average);
Marktotal= 0;
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.");
}
}

• 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. Feb 25, 2015 at 2:37

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;

public class ForNing {
public static void main(String args[]) throws IOException {
);

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

int studentCount = 0;
double classAverage = 0;

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

studentCount++;

int markSum = 0;
for (int i = 1; i <= 5; i++) {
}

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

}

classAverage /= studentCount;

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


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).
{"ft_lang_label":"__label__en","ft_lang_prob":0.8657748,"math_prob":0.8053818,"size":2033,"snap":"2022-05-2022-21","text_gpt3_token_len":439,"char_repetition_ratio":0.17151307,"word_repetition_ratio":0.022058824,"special_character_ratio":0.23954746,"punctuation_ratio":0.21393035,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9583521,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T05:21:50Z\",\"WARC-Record-ID\":\"<urn:uuid:6847e64e-f8ab-44d9-948a-ac34c049a569>\",\"Content-Length\":\"243849\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0d65d5a5-0819-44b6-bf96-ee42ecba5509>\",\"WARC-Concurrent-To\":\"<urn:uuid:59dc8fdf-d157-499b-884b-f01dc61e3063>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/82525/distinguishing-user-input-numbers-and-characters\",\"WARC-Payload-Digest\":\"sha1:FTTFQHKXBGW3A6W7XOIZV4E3PZDR5Q7F\",\"WARC-Block-Digest\":\"sha1:QUVKEQ4ONDSUJ6EPG7QPC27WDNBDDBG3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662555558.23_warc_CC-MAIN-20220523041156-20220523071156-00437.warc.gz\"}"}