Forums Register Login

Creating a proper if/else statement for this txt input

+Pie Number of slices to send: Send
Logically I'm having trouble on where and how to create an if/else for the user input. Simply I just want to give the user chances to re-enter there input if there input could not be found in the txt file. My first thought was to put the else statement in that while loop but that continue value is kinda interfering. Anybody have a clue? I would really love to get help because this is the last issue I have on my final project, then I'll finally have my creation done =)

+Pie Number of slices to send: Send
You need to wrap the code that asks for user input in a loop which is conditional on the input not being found.

Rather than trying to write this code straight off, write out the steps in English and then convert that to code.

BTW you really should sort out the format of your code, it's very easy to misread the while loop eg:
Should be something like:

or better still (but this does involve a code change rather than a re-formatting)

+Pie Number of slices to send: Send
On your second code box that's what I tried doing, I put an else in there but I get an error saying "else without if". When I got rid of continue it compiled fine but that messed up everything during runtime and I have no idea how to change up the code. Since I have no idea how to change up the code I want to leave that continue their. Is their another way to by pass and throw that else in their?
+Pie Number of slices to send: Send
Or are you saying I should create another while loop just to check if the user input is available?
+Pie Number of slices to send: Send
 

George Avilez wrote:On your second code box that's what I tried doing, I put an else in there but I get an error saying "else without if". When I got rid of continue it compiled fine but that messed up everything during runtime and I have no idea how to change up the code. Since I have no idea how to change up the code I want to leave that continue their. Is their another way to by pass and throw that else in their?



You can't put an "else" anywhere. It has to come right after the "if", or as you noticed, you will get an "else without if" compile error.

Henry
+Pie Number of slices to send: Send
I put it right after the if though is what I'm saying

+Pie Number of slices to send: Send
 

George Avilez wrote:I put it right after the if though is what I'm saying





Nope. That is not valid Java if/else syntax. Perhaps the Oracle tutorial is a good starting point for this... http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Henry
+Pie Number of slices to send: Send
It's not much of help sry. I know how to use if/else. Like I said the continue is throwing everything off for me, that if/else DOES work if there is no continue. There is no examples out there to show what to do after with that continue, unless I missed it. That's all I'm asking, first time using that value so idk how to properly syntax it...
+Pie Number of slices to send: Send
 

George Avilez wrote:It's not much of help sry. I know how to use if/else. Like I said the continue is throwing everything off for me, that if/else DOES work if there is no continue. There is no examples out there to show what to do after with that continue, unless I missed it. That's all I'm asking, first time using that value so idk how to properly syntax it...




To be blunt... if you know how to use if/else, then you know that the code you presented is *not* valid Java syntax. This means that you are presenting pseudo code, in order to convey what you want. Unfortunately, for me, I can't decipher what you are trying to do from the pseudo code. Perhaps it would help if you re-explain the logic that you are attempting to achieve?

And it has nothing to do with "continue". Replacing that line with any other valid statement will yield the same error.

Henry
+Pie Number of slices to send: Send
Well what the code is doing right now is asking for a user input, then the code will grab all the strings from the txt file and compare the users input with each string/line from the txt file. If there is a match then that string is deleted off the file. This works perfectly, but I want the code to also let the user know that there is no match in case the user enters an input that doesn't match with anything. ANd possibly loop them back to re-enter an input. As of right now if they enter an input that doesn't match then the program will still end correctly but no changes are made to the file.
+Pie Number of slices to send: Send
 

George Avilez wrote:Well what the code is doing right now is asking for a user input, then the code will grab all the strings from the txt file and compare the users input with each string/line from the txt file. If there is a match then that string is deleted off the file. This works perfectly, but I want the code to also let the user know that there is no match in case the user enters an input that doesn't match with anything. ANd possibly loop them back to re-enter an input. As of right now if they enter an input that doesn't match then the program will still end correctly but no changes are made to the file.




The pseudo code (the portion with the if/else) that you presented does string trimming, and printing. It doesn't do anything of what you described here.

Henry
+Pie Number of slices to send: Send
Yeah true. SO Am I better off just making a different while loop with an if/else that focuses on whether the input could be found on the txt file? Then break out of it if there was on found and take it straight to the while loop I have?
+Pie Number of slices to send: Send
 

George Avilez wrote:On your second code box that's what I tried doing,


Err, the second code box is syntactically exactly the same as the first code box. The only difference is the formatting.

George Avilez wrote:I put an else in there but I get an error saying "else without if". When I got rid of continue it compiled fine but that messed up everything during runtime and I have no idea how to change up the code.


I've no idea why you would even want to put an else in there, what are you hoping to achieve with this else clause?

George Avilez wrote: Since I have no idea how to change up the code I want to leave that continue their.


I've shown you the code to get rid of the continue so I don't understand why you "have no idea how to change the code".
+Pie Number of slices to send: Send
 

George Avilez wrote:Yeah true. SO Am I better off just making a different while loop with an if/else that focuses on whether the input could be found on the txt file? Then break out of it if there was on found and take it straight to the while loop I have?


You absolutely need a different loop if you want to repeatedly ask for an entry until you get one that is found. You can't do it with the one you currently have as that just loops over the lines in the file and for each user entry you need to repeat this process.
+Pie Number of slices to send: Send
 

You absolutely need a different loop if you want to repeatedly ask for an entry until you get one that is found. You can't do it with the one you currently have as that just loops over the lines in the file and for each user entry you need to repeat this process.



Thank you, this one makes sense to me and was basically what I was asking earlier when I asked:

Or are you saying I should create another while loop just to check if the user input is available?



Thanks for the help
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1118 times.
Similar Threads
Send a String to Server and server send it somewhere on network
How to end this loop
FTP Socket Programming
Unable to rename file
Trying to delete a string from a text file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:09:17.