• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping through a string

 
Charles Angemeyer
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Charles Angemeyer
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Angemeyer wrote:It does run, but always returns a zero instead of a value.

AvoidTheEqualityOperator
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Well spotted that man (and thanks for the plug). . . .
man???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic