Forums Register Login

Stuck on exercise 5.4 from Sierra/Bates book, please help

+Pie Number of slices to send: Send
Hello,

Could someone please have a look at my code and point out where I'm going wrong?

It compiles fine, but when I run it, I only get the expected output ("I like this food.") for the first element in the food array, "carrot". If I try to pass any of the other listed strings, "burgers", "rice" or "pizza", I get caught by the exception.

Thank you for your help,

Chandella
______________________________________

+Pie Number of slices to send: Send
The problem with your code is this :


What is happening is you are checking whether ONLY the first element of the array is a particular type of food BEFORE throwing an exception. If you have typed in "carrot" then the carrot is in fact the first element of the array so we have a good match hence the code breaks out of the LOOP. If the food you have typed in is "BURGERS" for example your code checks whether the first element of the array is "burgers" (first LOOP iteration). If it is you will get a correct response but if it is not you will get the EXCEPTION IN THE FIRST LOOP ITERATION. You never get to the second, third and so on because you are throwing the exception if the first command line argument is not the same as the first element of the array.

One way to fix that is to use a while loop and a boolean variable acting like a flag...Something from this sort:

+Pie Number of slices to send: Send
or
+Pie Number of slices to send: Send
That's awesome, thanks for your help, guys.

Boris, I tried your suggestion, but somehow kept getting stuck in an endless loop I suppose I have to play around with it a bit more to try to figure out what happened.

Achilleas, your solution was very simple, and spot on! And to think I spent the whole afternoon trying to solve it...

Thanks a lot!
+Pie Number of slices to send: Send
I have tried to code many different ways follow the way you coded, but it won't work. A better approach is have the "new BadFoodException()" out side the for loop like other have proposed, if it found it will print and return to the for loop, otherwise exit the for loop which will throw an exception.


Here is the complete solution, you can test and verify. Hope this help...



+Pie Number of slices to send: Send
Hello Guys, since we will need to know about the 'Console' class for the exams, I decided to experiment and modify Chandela's program since it has to do with reading user inputs and getting useful information out of the inputs and it worked perfectly! and made it possible to enter arguments as much as you like.

I decided to share it with you all. Please let me know if you have doubts, I'll try and clarify the doubts:

+Pie Number of slices to send: Send
Jacob's Console input is works perfectly..Scanner can be use as well if we want to find stuffs or tokenizaing data, I'd say a more versatile approach of getting input, use of Regex and tokenizing data. Haven't try it yet, but will try and see how it works...

+Pie Number of slices to send: Send
Tommy, your enhanced for-loop suggestion works perfectly as well. That's how I originally thought of coding mine, but because I was having problems, I ended up changing it to a normal for-loop.

Not that it ever made a difference, the problem was with the break on line 38, as well has having the exception as an else clause. Much better to have the exception outside the for-loop, bypassing it with a return statement if a match is found.

Ikpefua, thanks for your input. I haven't got to that part yet though I promise I will have a look at your code when I get to the relevant chapters in my book. Cheers!
+Pie Number of slices to send: Send
@Chandella...Please dont mention its my pleasure, no worries, java requires TIME and PATIENCE.
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1712 times.
Similar Threads
Exception not being caught
Exceptions program not working as expected
illegal start of expression when declaring method that throws an exception
exception
NetBeans passing entry parameter for main
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:57:18.