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);
}
}
Asked
Active
Viewed 167 times
-3

Sotirios Delimanolis
- 274,122
- 60
- 696
- 724

Urvaksh Mogrelia
- 1
- 2
2 Answers
0
Add a call to nextLine() after your nextInt() call to clear out the remaining "\n" that nextInt() leavers in the pipe.

Jon Kiparsky
- 7,499
- 2
- 23
- 38
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
- 3