-2
 Scanner input = new Scanner(System.in);
    System.out.println("Please enter the name of the first subject");
    String A = input.nextLine();
    System.out.println("Please enter marks in subject " + A );
    float a = input.nextFloat();
    **System.out.println("Please enter the name of the second subject");
    String B = input.nextLine();
    System.out.println("Please enter the marks of the subject "+ B );**
    float b = input.nextFloat();
    System.out.println("Please enter the name of the last subject");
    String C = input.nextLine();

In this code I expect that program to ask the name of second subject after taking in the value for first subjet marks but instead its printing both the print statement for second subject and ignoring to take the second subject name.

2 Answers2

0

If the name of the subject is only one word you should use input.next() instead of input.nextLine()

Try this:

 Scanner input = new Scanner(System.in);
        System.out.println("Please enter the name of the first subject");
        String A = input.next();
        System.out.println("Please enter marks in subject " + A );
        float a = input.nextFloat();
        System.out.println("Please enter the name of the second subject");
        String B = input.next();
        System.out.println("Please enter the marks of the subject "+ B );
        float b = input.nextFloat();
        System.out.println("Please enter the name of the last subject");
        String C = input.next();
Roger
  • 1
  • 1
-1

When you use Scanner functions that read tokens (e.g.: next(), nextInt(), nextFloat() etc.), the Scanner reads and returns the next token. When you read an entire line (i.e.: readLine()), it reads from the current position until the beginning of the next line.

use this

Scanner input = new Scanner(System.in);
            System.out.println("Please enter the name of the first subject");
            String A = input.nextLine();
            System.out.println("Please enter marks in subject " + A );
            float a = input.nextFloat();
            System.out.println("Please enter the name of the second subject");
            String B = input.next();
            System.out.println("Please enter the marks of the subject "+ B );
            input.nextLine();
            float b = input.nextFloat();
            input.nextLine();
            System.out.println("Please enter the name of the last subject");
            String C = input.next();
Optimizer
  • 116
  • 1
  • 1
  • 5
  • Thank you this indeed solved my problem for this code, but what if in case I actually needed to enter something which is not one word and it required me to used .nextLine(). – Rishav Sharma Jan 23 '22 at 11:51
  • And why it could execute .nextLine() properly for the first time..? since it ignores the .nextline() when this method is call second time. – Rishav Sharma Jan 23 '22 at 12:02
  • When you use Scanner functions that read tokens (e.g.: next(), nextInt(), etc.), the Scanner reads and returns the next token. When you read an entire line (i.e.: readLine()), it reads from the current position until the beginning of the next line. So you either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.next to consume rest of that line including newline. else you read the input through Scanner.nextLine and convert your input to the proper format (int, float etc) you need. – Optimizer Jan 23 '22 at 12:50