aspose file tools*
The moose likes Beginning Java and the fly likes Another Loop question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another Loop question..." Watch "Another Loop question..." New topic
Author

Another Loop question...

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Dec 05, 2005
Posts: 134
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!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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-


You down with OOP? Yeah you know me!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
You were right about using trim; sentence.trim() ought to work.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Dec 05, 2005
Posts: 134
@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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Loop question...