Mason Salcido
Mason Salcido

Reputation: 83

Scanner not reading string correctly

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.

Upvotes: 1

Views: 185

Answers (3)

abhishek sahu
abhishek sahu

Reputation: 648

Error is in scr.nextLine().equalsIgnoreCase("quit") because it was not assign user input to studentName.

Use and review the code below.

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");
    studentName=scr.nextLine();
    if(studentName.equalsIgnoreCase("quit")) {
        System.exit(0);
    } 

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

Upvotes: 0

LetsCode
LetsCode

Reputation: 198

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");
    String name = scr.nextLine();
    if (name.equalsIgnoreCase("quit"))
    {
        System.exit(0);
    }

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

hope u get it

Upvotes: 0

VHS
VHS

Reputation: 10174

Following statement consumes (but doesn't save) your first name:

if(scr.nextLine().equalsIgnoreCase("quit"))

The second name that gets saved in studentName variable is

studentName = scr.next();

If you want to store the first name too, you need to store it in a variable first.

Upvotes: 1

Related Questions