Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in inputs

Status
Not open for further replies.

JTan

Technical User
Oct 9, 2001
73
SG
Hi,

may I know what is wrong with my following codes? It works fine for the very first time when data is entered. However, for the next item to be recorded, it is not possible to read in the code at all. I tried to use System.out.flush() but it seems it is of no avail.

do {

System.out.flush();

System.out.println("\nItems sold: ");
System.out.println("==============");
try {
System.out.flush();

System.out.print("Enter product code: ");
System.out.flush();
code = stdin.readLine();
} catch (NumberFormatException t)
{
System.out.println("\nPlease enter the correct product code: ");
System.out.flush();
code = stdin.readLine();
}

try {
System.out.flush();
System.out.print("Enter quantity: ");
quantity = Integer.parseInt (stdin.readLine());
} catch (NumberFormatException s)
{
System.out.println("\nPlease enter the correct quantity: ");
System.out.flush();
quantity = Integer.parseInt (stdin.readLine());
}

try {
System.out.print("More items[Y/N]");
more = (char)stdin.read();
} catch (NumberFormatException r)
{
System.out.println("\nPlease enter the correct code: ");
System.out.flush();
more = (char)stdin.read();
}

System.out.flush();
count++;
Item item = new Item(code, quantity);

while (cnt < quantity) {
sold.addElement(item);
cnt++;
}//while
System.out.flush();

} while ((more == 'Y')||(more == 'y'));



 
hi, i dunno if it'll work but give it a try:

instead of flushing the output buffer try to flush the input buffer. something like System.in.flush(). tell me if it works.

luv
Karthik.
 
It's because you don't read in the whole line when you prompt for more input. You are leaving the \r\n in the input buffer and this gets picked up by the first readLine()

What you need to do is this:

more = stdin.readLine().charAt(0);

Regards,

Charles
 
vkarthik >> I dun think your code is compilable...:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top