This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Looping through a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping through a string" Watch "Looping through a string" New topic
Author

Looping through a string

Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
Hi, I'm working through a CodingBat problem and have come to an impasse.
The problem calls for identifying the last two characters of a string then determining how many times those two characters appear throughout other parts of the string. The last two characters are not counted.
For example, hixxhi returns a 1.
axxxaaxx returns a 2. Funny thing is, my solution looks similar to their solution but it still doesn't run.
I appreciate any help you might be able to give me.


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4161
    
  21

Charles Angemeyer wrote:but it still doesn't run.

What does that mean, do you get errors? Or do you get no output? Or do you get strange unexpected results?

My suggestion would be to take a pencil and paper, and a copy of the API. Go through each step of you program and write down what values will be stored in each variable at each stage of execution. See if you can determine where the mistake is.

Or if you want to get all technologied-up and are using an IDE, you could use a debugger witch break points.


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11139
    
  16

what exactly do you mean by "it doesn't run"? How do you know that? Do you get a compile error? does it crash? Does it report nothing? Does it report the wrong thing?

If you want help, you need to TellTheDetails (<---that is a link - please read that page).

Generally, I don't know how you can tell that the code you posted doesn't run. if you call the method, it runs. It may not be doing what you want, but the only way to tell what it is really doing is to a) use a debugger, or b) use a bunch of System.out.println() statements to see what it is doing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Charles Angemeyer wrote:The last two characters are not counted.
For example, hixxhi returns a 1.
axxxaaxx returns a 2. Funny thing is, my solution looks similar to their solution but it still doesn't run.

First: my basic advice is the same as Steve''s and Fred's.

Second: In your solution, the last two characters do appear to count.

Third: Your solution (once you get it running) will return 2 for axxxaaxx, but for the wrong reason (see above).

Fourth: There is a basic flaw in your loop logic.

Fifth: (TIP) Have a look at String.indexOf(String, int).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
Yep, my fault in the way that I described the issue. It does run, but always returns a zero instead of a value.
No matter, I will follow your suggestion to try and debug, that will give me a chance to learn the "debugger."
I'm using eclipse and I should've mentioned that too.
Many thanks.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3409
    
  12
Charles Angemeyer wrote:It does run, but always returns a zero instead of a value.

AvoidTheEqualityOperator


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Joanne Neal wrote:AvoidTheEqualityOperator

Well spotted that man (and thanks for the plug).

I'm still not sure how the loop manages to complete without an error though.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Winston Gutkowski wrote: . . . Well spotted that man (and thanks for the plug). . . .
man???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looping through a string
 
Similar Threads
Reading a String backward
A textfield that accepts only decimal numbers
substring to just get the last character
Quick String to <char> Array Conversion
How to write a rescursive algorithm?