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

Scanner skips nextLine() after nextDouble()

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      Line 11 is not stop to get the customerName
      If I move 3 lines as below
                      System.out.print("Again the cusomer name is " );
      customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);
      It will wait for input customerName the 2nd time


      import java.util.Scanner;
      public class SalesTax {
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      System.out.print("\n\n");
      System.out.print("The cusomer name is " );
      String customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);
      System.out.print("Again the cusomer name is " );
      customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);

      System.out.print("Enter purchase amount : ");
      double purchaseAmount = input.nextDouble();
      double tax = purchaseAmount * 0.10;
      System.out.println("Sales tax is $" + (int)(tax*100)/100.0);

      System.out.print("Again Purchase amoutn : ");
      purchaseAmount = input.nextDouble();
      System.out.println("Sales tax is $" + (int)(tax*100)/100.0);

      System.out.print("Again the cusomer name is " );
      customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);
      }
      }

      A DESCRIPTION OF THE PROBLEM :
      import java.util.Scanner;
      public class SalesTax {
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      System.out.print("\n\n");
      System.out.print("The cusomer name is " );
      String customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);


      System.out.print("Enter purchase amount : ");
      double purchaseAmount = input.nextDouble();
      double tax = purchaseAmount * 0.10;
      System.out.println("Sales tax is $" + (int)(tax*100)/100.0);

      System.out.print("Again Purchase amoutn : ");
      purchaseAmount = input.nextDouble();
      System.out.println("Sales tax is $" + (int)(tax*100)/100.0);

      System.out.print("Again the cusomer name is " );
      customerName = input.nextLine();
      System.out.println("The cusomer's name is " + customerName);
      }
      }



      FREQUENCY : often


            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: