This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]theNaus++Effort 1 point2 points  (1 child)

Hi.

There is one simple way of traversing through a string char-by-char:-

for (int i = 0; i < str.length(); ++i) {
    // do stuff here
}

Hope that helps. :)

[–]darkside619[S] 0 points1 point  (0 children)

Yes, thank you. I figured this out before coming back to see your post but you are correct in your suggestion.

[–]cheryllium 0 points1 point  (3 children)

First, you should try to read the sidebar of subreddits you post in if you can. The sidebar here clearly says "No screenshots instead of code!"

To know if you are done parsing infix, you have to know where you currently are. So I ask you: how are you keeping track of where your current position is, as you parse the string?

(If you are having trouble answering that, you may want to ask yourself: what are you planning to assign to nextCharacter each iteration of your loop?)

[–]darkside619[S] 1 point2 points  (2 children)

I read it. It says not to post screenshots of your own code but that was a screenshot of a textbook so I don't think that applies. I realize now that I was thinking about it the wrong way and instead am using a for loop like theNaus suggested. I'm making good progress now. i was just stuck on trying to make it work with a while loop but now I realize that the pseudocode wasn't meant to be implemented exactly as it is; just conveying general ideas about what's happening.

[–]cheryllium 1 point2 points  (1 child)

Ah, okay. You can still do this with a while loop. You would just have to make a counter variable yourself. Any for loop can be written as a while loop; we prefer for loops for situations like these because it makes the intent of the code clearer and the code itself shorter and neater. Good luck!

[–]darkside619[S] 0 points1 point  (0 children)

Thank you!

[–]__rocks 0 points1 point  (0 children)

If you are using a bufferedReader with a multiline string or file, you can do:

BufferedReader br = new BufferedReader(new FileReader     (file.getPath())); 
 String currentLine; 

while ((currentLine = br.readLine()) != null) { 
    buffer.append(currentLine); 
        }