-3
import java.util.Scanner;
class bazar
{
    void calculate ()
    {
        int sum=0;
        Scanner sc = new Scanner (System.in);
        System.out.println("Hi ! welcome to out advance calculator");
        System.out.println("Enter the number of items that you wish to compute");
        int c = sc.nextInt();
        String item[] = new String[c];
        int price[] = new int[c];
        for (int i=1; i<=c; i++)
        {
          System.out.println( "please enter the item name : " );
          item[i] = sc.nextLine();
          System.out.println();**// i need to wait for input before proceeding**
          System.out.println();
          System.out.println();
          System.out.println( "please enter the price of " +item[i]+":");
          price[i] = sc.nextInt();
          sum=sum+price[i];
        }
        //display part 
        for (int k=1; k<=c; k++)
        {
            System.out.println(  "ITEM                       PRICE");
            System.out.println (item[k]+"                  "+price[k]); 
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("YOUR BILL TOTAL HAS COME TO----------------->"+sum);
    }
}
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683

2 Answers2

0

Add a call to nextLine() after your nextInt() call to clear out the remaining "\n" that nextInt() leavers in the pipe.

Jon Kiparsky
  • 6,854
  • 2
  • 20
  • 37
0

Scanner nextInt() (and similar) functions does not read newline entered after user input string. So it remains in buffer. However nextLine() function immediately returns when receive a new line character. To resolve the problem ensure that none of the newline character is left in buffer. The quickest way will be use a nextLine() call after each call to nextInt() and similar functions. Alternatively avoid using nextLine() and use next() function. But it cannot read multi word string.

nileshg
  • 23
  • 2