the code in this post is meant to take 2 words (separated by a space) through user input and pass these two values to a switch case. the split() method, for some reason, would only store the first word before the space and not the second, thus the following code was implemented.
for some reason, would only store the first word before the space and not the second
That's very odd. Are there maybe cases with only one word, or one word with a space? Using split(" ", 2) might help there, as it will give an empty string in those cases.
the names of the items in my arraylist, when applicable, were separated by an underscore instead of a space. I tried debugging the split method for a while but couldn't get the second word to appear at index 1, only the first word at index 0. the solution above is by means clean, but it worked as a viable solution for the assignment.
edit: for cases with one word, that string is handled in the switch case with a matching string.
I tried debugging the split method for a while but couldn't get the second word to appear at index 1
was there an index 1? what was in it? if you had two spaces in a row then split[1] returns an empty string, and your copy variable would have a space first then the second word which could give intended results depending on how you use the copy string.
we don't see the rest of the code. What it does so far is valid. Split is stored in an array, copy could be something used downstream. I'm not sure how it's bad code. Naming could be better, but again, we don't see even the whole function to judge that.
That whole chunk could be entirely eliminated and the if statement changed to:
if (input.startsWith("examine")) {
input = "examine";
}
There's no reason for any of that other stuff. If you need them then this could should be in a separate method because its probably already doing too much.
how do you know splitIn and copy aren't used later?
Your code is certainly wrong.
it needs an
else { input.replace("examine ", "") }
this whole thing looks like a psrsert for some high level expression. Not sure why they needed it, and maybe it was a bad idea, but I'm sure there's hella lot of logic down the line
The else isn't required, you need to read the java documentation on what replace does.
While I don't know for sure that the variables are used I'm 99.99% sure. That's a very clunky way of trying to match the first word in a string, whoever did this doesn't seem to understand how regexes or basic searching work. I'm betting copy is a leftover of something else they tried. As a programming instructor I see this sort of bogus construction all the time.
yes, I realized this implementation a couple hours after it was turned in. You can just replace examine and use the remaining txt as your parameter. I will post the entire, original code as well here soon.
good point here! this entire block ended up being removed in the final project as it was unnecessary, but this would've worked had we needed preprocessing.
24
u/[deleted] Nov 05 '21
the code in this post is meant to take 2 words (separated by a space) through user input and pass these two values to a switch case. the split() method, for some reason, would only store the first word before the space and not the second, thus the following code was implemented.
7
u/RedditAcc-92975 Nov 05 '21
we don't see the rest of the code. What it does so far is valid. Split is stored in an array, copy could be something used downstream. I'm not sure how it's bad code. Naming could be better, but again, we don't see even the whole function to judge that.
10
u/nekokattt Nov 05 '21
The most horrifying thing here is the lack of a space between the if and the parenthesis.
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 05 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/_LIM10_ Nov 06 '21
Stupid question but whats "init"
1
u/reddit-testaccount Jan 03 '22
at least use input.substring(check.length() + 1) than it would be ok
3
u/Kasufert Nov 06 '21
Can we get a full Function for this one
2
u/ososalsosal Nov 06 '21
This person has been fighting a framework bug for 8 hours and can no longer see straight.
1
u/m2ger Nov 08 '21
Btw, for those who didn't know, String.split() can have surprising behaviour.