• Post Reply Bookmark Topic Watch Topic
  • New Topic

Am I losing an iteration?  RSS feed

 
Rob Craigson
Greenhorn
Posts: 3
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry if the subject line is vague and/or misleading; it's gotten fairly late and I'm struggling in the whole "thinking" department.
I've run into a strange issue; it is entirely possible that it is not specifically java-related and that I'm just overlooking a simple programming problem, but this has had me stumped for the greater portion of an hour. I'm also not entirely sure if this would be considered a beginner question or not, but I have a real good feeling I'm making a rookie mistake somewhere in here that I just can't manage to find.


The issue, simply put: A simple counter is not counting appropriately, I think?

My code is as follows:



Ignore references to postings if you can help it, I dont think that's relevant to the problem (correct me if you find something!). Simply put, the program should just be counting instances of a word/string/whatever and adding 1 every time it sees one.

With the input being:
A B C
A F G
A F M
A R F


The output will look like:

Name: A
Name: 1
------
------
Name: B
Name: 1
------
------
Name: C
Name: 1
------
------
Name: A
Name: 1
------

------
Name: A
Name: 2
------
------
Name: F
Name: 1
------
------
Name: G
Name: 1
------

------
Name: A
Name: 3
------
------
Name: F
Name: 1
------
------
Name: M
Name: 1
------

------
Name: A
Name: 4
------
------
Name: R
Name: 1
------
------
Name: F
Name: 2
------



As you can see, A's "df" ( a terrible naming convention, to be sure ) is printed as 1 twice, and only seems to actually increment on the third instance (becoming 2?). Why could it be doing this?

I'm sorry for a very lengthy post, but I saw no better way to explain the pickle I've found myself in.


Thank you in advance for even reading! I will post a reply myself if I manage to see what I'm doing wrong.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Did you mean to have return in the middle of a loop? You know that will terminate the whole method?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if it explains exactly what you are seeing, but I can see one mistake. Look at lines 33/34. You create two Index objects, add one of them to the list, and return the other one.

So fix that and then see what the output looks like.

Unrelated to your specific problem, you might want to look at the HashMap class. That will make your lookup simpler, because it allows you to index your collection by a string, so you don't have to iterate through it each time.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you marked everything static?
Why are you using a for loop in the second method rather than a for‑each loop?
Did you mean to start the counter at 1 in the last method? When you get there you have not seen anything yet, so shouldn't it be 0?
Did you know you can pass a String to a Scanner and let it split on spaces rather than using String#split? Did you realise you can get wrong results from that regex (single space) if you have any double spaces in your input?
 
Rob Craigson
Greenhorn
Posts: 3
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Did you mean to have return in the middle of a loop? You know that will terminate the whole method?


Hi, thanks for the welcome!
To address your questions one at a time:

I'm assuming you're referring to


The idea was to terminate the loop once I've found the appropriate Index; I didn't see a reason to iterate further. There may be a better way to do this, I'm always willing to learn!


Why have you marked everything static?

When I wrote getIndex and tried to call it in main, Eclipse told me ahead of time with those nice squiggly lines that I can't access a nonstatic method (Index.getIndex) inside of a static method (main, of course) and so I fixed that. I proceeded to mark most/all things after that static to avoid having that trouble. To be frank, and this is probably clear from my code/response, I don't *fully* understand the issues with calling static methods (I've noticed that I was able to change countInstances from static and it still works just fine, but I don't know why). I have looked at a few stackoverflow pages on the matter, but I have not studied in depth, which is my own fault. If anyone feels like giving me a lesson / example as it pertains to code I can relate to (hint hint), I would listen indeed!

Short answer: because I'm a nub.


Why are you using a for loop in the second method rather than a for‑each loop?

Because I am not used to using arrays in java; it really just didn't enter my mind.


Did you mean to start the counter at 1 in the last method? When you get there you have not seen anything yet, so shouldn't it be 0?

For the sake of the problem at hand I didn't see it mattering to leave it in like that, but it should definitely be 1 for reasons not worth explaining / left in the code.

Did you know you can pass a String to a Scanner and let it split on spaces rather than using String#split? Did you realise you can get wrong results from that regex (single space) if you have any double spaces in your input?

I had not realized that, nor did I know the scanner had that capability; the input for this program will only ever be single-space, but that's a great tip to keep in mind regardless! Thank you very much!


Good questions, good tips.

Thanks again!
 
Rob Craigson
Greenhorn
Posts: 3
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'm not sure if it explains exactly what you are seeing, but I can see one mistake. Look at lines 33/34. You create two Index objects, add one of them to the list, and return the other one.

So fix that and then see what the output looks like.

Unrelated to your specific problem, you might want to look at the HashMap class. That will make your lookup simpler, because it allows you to index your collection by a string, so you don't have to iterate through it each time.


Hey, thanks for your response!

Yeah, the mistake you found definitely fixes it. I needed to be returning the instance that I create, not a whole new one; that would explain why my program was always ignoring only the first of every token it found. Thank you so much!

In regards to hashmap, I looked it up, and it does look like it will make everything so much easier. I will keep playing around with it and I should be good to go for questions now.

Thanks all for your help,
I'll be sure to pay it forward (if I can!)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Craigson wrote:I'll be sure to pay it forward (if I can!)

No bribes allowed on JavaRanch.

Winston
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . No bribes allowed on JavaRanch. . . .
Except possibly in beer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!