last day (15 days later) » 

5:09 PM
0
A: How would i search a certain word after a character in java ?

Kieran LavelleSo then John, let me guess. You're a computer Science student at the university of Warwick. Here you go, String s = "hello #yolo blaaa"; if(s.contains("#")){ int hash = s.indexOf("#") - 1; s = s.substring(hash); int space = s...

 
This could fail. The input may not contain any #hashtags.
 
True I'll edit it now
 
Think again. What will s.substring(space) return?
 
The index of the first space found after the hashtag is stored in space, this creates a string from the hashtag to the end of the word.. I believe I know the coursework which he is doing and if thats the case then there is always a space after it so this assumption can be made for simplicity.
 
Okay, let's assume your assumption is correct. There are still two grave mistakes you're making. Should I spill the beans? I'd like if you're able to figure it out :)
 
5:09 PM
Spill the beans, I see nothing.
Moved it here
 
Okay, the first issue is
hash = indexof # - 1
is it correct?
 
indexOf("#") - 1; does work yes
 
no it will move the index to the space just before hash
it needs to be indexof + 1
 
Yes it will do this to include the hash, because what he is actually trying to do is make a twitter application where it should have the #. Whilst its not what he asked it is what he needs.
 
if you want to include the hash then it needs to be just indexof!! No, - 1
"012#456".indexOf("#") = 3
substring(hash) = #456
substring(hash-1) = 2#456
 
5:18 PM
Unfortunately the -1 needs to be there otherwise you get a compile time error with the programme he's working on
 
Why don't you run your program and see all the errors for yourself?
Your program will print: " #yolo blaaa"
Fortunately the concepts don't change, the string methods will do as documented. It's not dependent on the program he's working on.
 
Oh no I know it isnt but what I do know, is for the coursework he has to do that works. It worked for me and I got the marks for it.
 

last day (15 days later) »