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

unable to take string input after interger input using Scanner class

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      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


        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: