• Post Reply Bookmark Topic Watch Topic
  • New Topic

findWithinHorizon(".", 0).charAt(0); is really bugging me.  RSS feed

 
Joseph Alrawi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello readers!

After hours of working on this code, I really can not understand what "findWithinHorizon..." does. Well I understand that it looks for a character based on what you typed in "charAt(number);". So far so good. But the problem is, it acts really, really strange. I have a code, not too long that I've been working with for a while now, it is very simple, but still complicated for me since I'm a beginner.



First of all, the program needs to scan 2 lines in order for it to move on, which has probably something to do with 2 scanner being called (letter and word one). The point of this program is simple, it scans for a word typed by the user. The program grabs a random letter in that word and then requires the user to type the correct letter in the command box. So the issue is, as mentioned just now, it needs to scan twice, so I have to type two lines. Second of all, it only works with words being 2 or less letters long, if I try something with 3 letters it returns:

Hello, please type any word.
fff
fff
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at Default.Horizon.main(Horizon.java:27)
Picked up _JAVA_OPTIONS: -Xmx512M (what is this anyways?)

I hope this wasn't too much bother-some for you to read, any help greatly appreciated, thanks!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please, UseCodeTags (← click this) when posting. I added them for you this time. Doesn't it look better?

Have you looked at Scanner#findWithinHorizon javadoc? (link here)
It describes what the method does.

It has nothing to do with charAt method. This method is called on a result of findWithinHorizon which happen to be a String.

Print out whatever is returned from findWithinHorizon and you will see what it is.

From the exception message I can see that you want to extract a character at index 1 from a string that is of length 0 or 1 which causes the exception.

The method findWithinHorizon workd with Pattern class (as javadoc says). Pattern class works with regular expressions (or regexes).
A dot character "." in a regex has a special meaning. Are you aware of that?
 
Joseph Alrawi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Welcome to the Ranch!

Please, UseCodeTags (← click this) when posting. I added them for you this time. Doesn't it look better?

Have you looked at Scanner#findWithinHorizon javadoc? (link here)
It describes what the method does.

It has nothing to do with charAt method. This method is called on a result of findWithinHorizon which happen to be a String.

Print out whatever is returned from findWithinHorizon and you will see what it is.

From the exception message I can see that you want to extract a character at index 1 from a string that is of length 0 or 1 which causes the exception.

The method findWithinHorizon workd with Pattern class (as javadoc says). Pattern class works with regular expressions (or regexes).
A dot character "." in a regex has a special meaning. Are you aware of that?



Hey! Thanks for your help! I tried using findInLine instead since it really made it a whole lot simpler. But what can I do so the program only needs to scan one line and not 2 in order to fetch the word and the character? Meaning this:

Hello, please type any word.
Hello //this bugs me.
Hello
l
You have answered:
Hello

What letter is at the number 4 of the word you typed?
--------------------------------------------------------------------
Thank you for your time and the warm welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!