Trouble with char

User defined exit from loop

Posted 20 April 2009 - 09:54 AM

Here is the code that I have written. I am having trouble with requesting the user to continue the loop or exit. Any help would be greatly appreciated. I have narrowed the problem down to the following bit of code with in the whole project.

Here is the problem child. Any ideas?

System.out.println("Do you Have more Shopping? Y to continue any other key to exit: ");
prod = (char)input.nextInt();

Re: User defined exit from loop

Thanks, I didn't see that problem. However, I am still having problem with this line of code.

prod = (char)input.nextInt();

I am pretty sure that this is causing my error but am uncertain as to what is wrong or how to fix it.

here is the error that I recieve when I debug it in NetBeans

debug:
Enter Product Number 1-5: 1
Enter Quantity Sold: 2
Total Sales for this Product: $5.96
Do you Have more Shopping? Y to continue any other key to exit:
y
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at prodsales.main(prodsales.java:82)
Java Result: 1

Am I even writing this code correctly? I have a hard time with the char command. Does anyone see where I am going wrong?

Re: User defined exit from loop

Posted 21 April 2009 - 09:52 AM

NeoTifa, on 20 Apr, 2009 - 06:25 PM, said:

Did you not even read my post?

Sorry NeoTifa,

I did not see your post. However, when I tried to a next line with the prod being a string I got an error of incompatible types (String and char). This is what I have been fighting with the whole time.