I am working on this problem and I seem to be having a weird bug... When ever I am trying to store a string using .nextLine(); it creates a new line and saves the 2nd line.
My Code: public class Averages {
public static void main (String args[]) {
String studentName = "";
int score1, score2, score3;
float Average = 0f;
int numberTests = 0;
Scanner scr = new Scanner(System.in);
System.out.println("Enter students name");
if(scr.nextLine().equalsIgnoreCase("quit")) {
System.exit(0);
} else {
studentName = scr.next();
}
System.out.println("Enter the amount of tests");
numberTests = scr.nextInt();
System.out.println("Enter students scores");
score1 = scr.nextInt();
score2 = scr.nextInt();
score3 = scr.nextInt();
Average = (score1 + score2 + score3) / numberTests;
System.out.print(studentName + " " + Average);
}}
The error is the first input is not being saved but will go to another line and save that instead. When being ran this is the output:
Enter students name
Stack
Overflow
Enter the amount of tests
3
Enter students scores
1
2
3
Overflow 2.0
I understand the math is wrong but I need to figure out the name portion first if anyone can tell me what i'm doing wrong I would greatly appreciate it.