1

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.

  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Mar 10 '17 at 03:44
  • I read through that, I understand it but I am not capturing nextInt first so it should be getting the first string given am I wrong? – Mason Salcido Mar 10 '17 at 03:49

3 Answers3

1

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.

VHS
  • 8,698
  • 3
  • 14
  • 38
0

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

vineeth
  • 178
  • 1
  • 13
  • *hope u get it* Maybe I get, maybe the OP gets it, but this type of answer is not very useful for a beginner or someone not familiar with this code. Please state what you have done. – Scary Wombat Mar 10 '17 at 05:15
  • @ScaryWombat Well said... So here is the explanation... The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. So Scanner.next() will wait for the next entry .. This is why you Skip the First string... – vineeth Mar 10 '17 at 05:20
0

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);
}}
Pang
  • 8,605
  • 144
  • 77
  • 113