Ways to read input from console in Java

Reading Input in Java

Why is Scanner skipping nextLine() after use of other next functions?

Program to Read and Print the name of the user


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
  public static void main(String args[]) 
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the String");
    String name = input.nextLine();
    System.out.println(name);
  }
}

Program Output
Enter the String
satish
satish 

Program to Read and Display an Integer Value to the User.


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the number");
      int value = input.nextInt();
      System.out.println(value);
    }
  } 

Program to Read a Character and Display the character to the User


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the character");
      char inputChar = input.next().charAt(0);
      System.out.println(inputChar);
    }
  }
  
  Program Output
  Enter the character
  b
  b 

Program to Read a boolean value from the user


  package bbsProject;
  import java.util.Scanner;
  public class bbsdemo {
    
    public static void main(String args[])  
    {
       System.out.println("Java is Object oriented? Enter true/false");
        Scanner input = new Scanner(System.in);
        boolean answer = input.nextBoolean();
        if(answer)
        {
          System.out.println("You are Correct");
        }else
        {
          System.out.println("You are wrong.Learn Java from CodeSpindle");
        }
    }
  }
  
  Program Output
  Java is Object oriented? Enter true/false
  false
  You are wrong.Learn Java from CodeSpindle 

Program to Read a Float value from the user


  package bbsProject;
  import java.util.Scanner;
  public class bbsdemo {
    
    public static void main(String args[])  
    {
       System.out.println("Enter a float value");
        Scanner input = new Scanner(System.in);
        float number = input.nextFloat();
        System.out.println("The value entered is "+number);
    }
  }
  
  Program Output
  Enter a float value
  23.55
  The value entered is 23.55 

Program to read a String, int and a String using the same Scanner Object


    package bbsProject;
    import java.util.Scanner;
    public class bbsdemo {
      
      public static void main(String args[])  
      {
        System.out.println("Enter your name");
          Scanner input = new Scanner(System.in);
          String name = input.nextLine();
          System.out.println("Enter the mark");
          int mark = input.nextInt();
          System.out.println("Enter the address");
          String address = input.nextLine();
          System.out.println(name+""+mark+""+address);   
      }
    }
    
  Program Output  (This programs leads to an error while reading data)
  Enter your name
  satish
  Enter the mark
  12
  Enter the address
  satish12
Reason for the issue: As we can see the address is skipped and not being read.
  • The issue with the program not reading the third input (address) arises because of how Scanner handles different data types.
  • Here's the breakdown:
    • input.nextLine() for name: This reads a line of text (including spaces) until the user presses Enter.
    • input.nextInt() for mark: This reads the next integer value entered by the user. However, it only consumes the integer itself, leaving the newline character (\n) entered after the mark in the input stream.
Solution to the above Error
  • Consume the newline character: After reading the mark with nextInt(), add a call to input.nextLine() to discard the leftover newline character. This allows the next nextLine() for the address to work correctly.
  • 
          package bbsProject;
          import java.util.Scanner;
          public class bbsdemo {
            
            public static void main(String args[])  
            {
              System.out.println("Enter your name");
                Scanner input = new Scanner(System.in);
                String name = input.nextLine();
                System.out.println("Enter the mark");
                int mark = input.nextInt();
                input.nextLine();//line added to discard the newline character
                System.out.println("Enter the address");
                String address = input.nextLine();
                System.out.println(name+""+mark+""+address);   
            }
          }
    
    Program Output
    Enter your name
    Satish
    Enter the mark
    12
    Enter the address
    Vellore
    Satish12Vellore
    
  • Use nextLine() consistently: Instead of nextInt(), you can use nextLine() for both name and mark. Since nextLine() reads the entire line, it will capture the integer entered by the user even if it doesn't contain any spaces. Then, you can convert the captured string to an integer using Integer.parseInt().
  • 
    package bbsProject;
    import java.util.Scanner;
    public class bbsdemo {
      
      public static void main(String args[])  
      {
        System.out.println("Enter your name");
        Scanner input = new Scanner(System.in);
        String name = input.nextLine();
        System.out.println("Enter the mark");
        String markStr = input.nextLine();
        int mark = Integer.parseInt(markStr);  // Convert string to integer
        System.out.println("Enter the address");
        String address = input.nextLine();
        System.out.println(name + mark + address);   
      }
    }
    
    Program Output
    Enter your name
    Satish
    Enter the mark
    12
    Enter the address
    Vellore
    Satish12Vellore