• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Loop question...

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
need help.
whenever i run my programme i get following output:

Please type in a sentence and press [ENTER] : lets go
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 4
at java.lang.String.charAt(String.java:558)
at charCount.main(charCount.java:30)
. . .




once again...
i dont know where I'm going wrong. Can anyone offer some sugeestions?

here's the code:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All AnythingOutOfBoundsExceptions mean there is an error somewhere, usually in the coding. But I tried it and it ran without any exceptions.
Try again.

Why have you put so much coding in the main method? You ought to move that coding into another method, or better into another class, and use the main method to set it off.
You don't initialise IM anywhere; I tried it with a Scanner object.

CR
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the IM (Input Module) class in my directory, from where all it's methods can be accessed by my programme.....

i'm doing everything in main so i can have a good overvirew,....ofcourse i'll correct that later....

can anyone please help me with this code here?....thanx!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very good starting point to find your problem is to use the exception that was thrown.


Please type in a sentence and press [ENTER] : lets go
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 4
at java.lang.String.charAt(String.java:558)
at charCount.main(charCount.java:30)
. . .



Here it says that the problem is in the main method of the charCount class. It is at or near line 30 in the charCount.java file.

Somewhere near there is a call to the charAt() method, with an index request of 4 -- it throws this exception because the string that is taking the charAt() of, has a length of less than 5.

Henry
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay,

i've completely rewritten my prog.,...and it works now..

i was making a mountain out of a mole hill,
sometimes you need to think simple :-)

however i still have one question:
if i want to "trim" my sentences in such a way that the first and last (or more) 'space' characters (at the end or preceding the actual words) are ignored when being read in,...

what method would come into question?

i was thinking for example:...

1. String sentence IM.readString(); and then....
2. sentence.trim();

isn't this correct?
'tried using it, but it seems not to wok, the method doesnt seem to be implemented.

any hints as to why?

thx!
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ive never heard of im.readstring, or string.trim();

umm why not just scan the input, and then for loop the string and count

each char. as i increments?

this is much much easier

-Justin-
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wolfgang Obi:

however i still have one question:
if i want to "trim" my sentences in such a way that the first and last (or more) 'space' characters (at the end or preceding the actual words) are ignored when being read in,...

what method would come into question?

i was thinking for example:...

1. String sentence IM.readString(); and then....
2. sentence.trim();

isn't this correct?
'tried using it, but it seems not to wok, the method doesnt seem to be implemented.

any hints as to why?

thx!



sentence.trim() will work but it doesn't actually change the String that sentence is referencing. It creates a new String object and returns that, so you need to change the code to
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were right about using trim; sentence.trim() ought to work.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Justin Fox,

I am really surprising by your comments... I didn't get you what you are trying to do...

ive never heard of im.readstring, or string.trim();

umm why not just scan the input, and then for loop the string and count

each char. as i increments?

this is much much easier

-Justin-




Now pls take care while answering the threads
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Justin....
EXACTLY what i did *SMILE*!.....
really nice to know you're suggesting the same solution ended up using, 'means I'm slowly beginning to understand what exactly i'm doin!

@Joanne ....
wonderful!
i almost read through the whole API!....been a great help :-)))

the programme's working 100% the way i want it to....!

ya'll are too much.

thx!
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic