i wanted to ask, to the holy and patients ones, to take a look at this code and help me, if possible, with some questions.
This is a program i'm creating to help me learn and remember various method of String and String Builder.
The program asks the user to insert a String, then asks to answer correctly on the result of a given String method
(indexOf in code reported) applied to the string inserted. If the answer is not type-correct (in indexOf has to be a number)
the program asks the same question again.
The value of the parameters are created dinamically based on the string inserted by the user.
The code works fine.
My questions are about the better way (if there is a better way) to implement this code.
After editing three times (cursed the back button of the browser grrrrrr...), i've tried to lighten the most the topic.
Hope it's a bit better.
Ok, here is the code:
Now, here the questions. One last premise. I'm trying to learn to code properly, so i'd like to understand if my choices are correct. Here the question/doubts.
1 - in method checkAnswerInput() (which controls if the user has typed a number and not a letter),
there are two different implementations i've made to call back again the question, one with switch
and one with if else. I've heard that it's not good to use switch, but in this case i think
is the better solution. What do you think?
Are there other possible solutions to overcome this problem?
2 - In the four method that calls String methods (indexOfParamInt...() etc.) there is an if construct that repeats itself.
is there a possibility to use a method instead of four repetitions?
the point is that i need a return statement inside the method, only if the user typed a letter,
but if i create a method to handle the task, the return statemente works on the outside method,
not on the calling method. are there other possibilities to do this?
ok, for now that's it..
sorry if the questions seems dumb, probably they are.
if someone wants to give me feedback in general, they are more than welcome
thank you for the attention
Alex Derek wrote:i wanted to ask, to the holy and patients ones, to take a look at this code and help me, if possible, with some questions.
Well, it would help an awful lot if you could break up those enormous lines. It makes your thread (and code) very hard to read.
I'd do it myself, but you have tons of them. Just use the 'Edit' button, and the 'Preview' facility before you post.
(Tip: Don't put code and comments on the same line)
The rule here is:
80 characters max (the SSCCE (←click) page actually recommends 62)
I suggest you re-read the UseCodeTags (←click) page carefully; and also the DontWriteLongLines one.
Isn't it funny how there's always time and money enough to do it WRONG?
Thank you for your reply.
I've tried to adjust a bit the question. I'm not sure it's ok.
If not, please tell me what i should improve.
@ Ivan, my comments were not that good, i've changed them, now they should be more clear.
The input string it's this:
My expression was not right.
The program works so:
Please insert a word, letters and numbers allowed: <user typing> want a beer?</user typing>
What is the result of method "indexOf(int ch)"
indexOf( ) applied on your string? <user typing>0</user typing>
Sorry, the correct result is "4". try again!
What is the result of method "indexOf(int ch, int fromIndex)"
indexOf(n, 2) applied on your string? <user typing>2</user typing>
What is the result of method "indexOf(String str)"
indexOf(wa) applied on your string?<user typing> f</user typing> -> user inserted a letter,
not good, ask the question again Sorry, but this method requests an integer as answer, so you have
to insert a number. Try again!
What is the result of method "indexOf(String str)"
indexOf(ant a) applied on your string?<user typing> 1</user typing>
What is the result of method "indexOf(String str, int fromIndex)"
indexOf(, 2) applied on your string?<user typing> 0</user typing>
Sorry, the correct result is "2". try again!
@ Winston, i've tried to clear the code. I'm sorry but it's still more than a hundred lines.
Alex Derek wrote:@ Winston, i've tried to clear the code. I'm sorry but it's still more than a hundred lines.
Well, thanks for the effort, I'll look it over thoroughly and give you any more comments as they come to me (probably tomorrow, I'm afraid; it's nearly 11pm here), but for future reference you may want to look at the SSCCE (←click) page. It's an excellent guide to how to provide code to forums like this.
However, one thing that does spring to mind immediately is that when writing a program - especially one that is arranged in "methods" (ie String methods) like yours:
1. Don't write more than about 10 lines at a time without compiling.
2. Break up each "method" into its own method (or class), and don't even start on a new one until you are absolutely sure that the current one works completely - ie, you shouldn't suddenly be finding errors when you're working on your 4th method.
Since the pattern for each "method" (actually, from what you were saying, a "method lesson") is the same (take string→take parameter(s)→take user's answer→check answer against actual result), writing and testing each one individually might have pointed you in a different direction for constructing your program. For example:which you then implement for each String method you want to create a "lesson" for.
Unfortunately, you appear to have worked on all of them at once, which is why you're worried about dispatch statements like if...else and switch. You may still end up using one, but when you find them cropping up a lot in your programs, it's usually an indication that you haven't properly decomposed your problem.
BTW, the above is just ONE possibility; there are many others. You could also eliminate the Scanner parameter by simply passing it to each "lesson" object when you create it.