• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic