Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8277174

unable to take string input after interger input using Scanner class

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      in all java versions

      A DESCRIPTION OF THE PROBLEM :
      Scanner class does not recognize the new line character after taking input in int so it skips the String input.

      REGRESSION : Last worked in version 8u311

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      take input in int variable through scanner object and then take input in string object using same scanner object.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      it will skip the string input
      ACTUAL -
      it will skip the string input

      ---------- BEGIN SOURCE ----------
      import java.util.Scanner;
      class Main{
      public static void mian(String args[]){
      Scanner inp = new Scanner(System.in);
      int num;
      String name="";
      num = inp.nextInt();
      name = inp.nextLine();
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      by making a new object Scanner class

      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: