This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a proper if/else statement for this txt input

 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 =)

 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or are you saying I should create another while loop just to check if the user input is available?
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put it right after the if though is what I'm saying

 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
George Avilez
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic