0

My program needs to read a string and process each character, one at a time. I don't want to put the string into a char array, so is there any way to check for the end of the string without doing this? I tried to do this (where s is a String): s.charAt(index) != null as a condition for a while loop, but I shortly figured out that this obviously doesn't work, because you just can't use != with char and null. Is there any other way?

5
  • 5
    for (int index = 0; index < s.length(); index++)... Commented Mar 27, 2014 at 3:21
  • Are you aware that a String already is a char array? Commented Mar 27, 2014 at 3:21
  • I'm really not sure what you mean by "checking for the end". Strings aren't null-terminated, so the fact that a String is a size > 0 means that there's an end at the last character. Commented Mar 27, 2014 at 3:21
  • Just call s.length() to determine the length of the string!! Commented Mar 27, 2014 at 3:24
  • Oh yes, I understand now, thank you. (I'm an amateur Java programmer, so please forgive the silly mistakes...!) Thank you, MadProgrammer, that was the solution... Commented Mar 27, 2014 at 3:24

6 Answers 6

3

You could try comparing your index variable to s.length():

while (index < s.length()) {
    //process
}
Sign up to request clarification or add additional context in comments.

Comments

3

You don't need to "put the string into a char array", you can use the string length as a limiter, and charAt to process each character:

for (int chPos = 0; chPos < str.length(); chPos++)
    doSomethingWith (str.charAt(chPos));

Comments

3

You need to use String's .length() method.

String s = "test";   
for(int i=0; i < s.length ; i++){
   char c = s.charAt(i);
}

Comments

3

You can use the String's method length to check the size of the string. for example:

for(int i = 0; i < s.length(); i++) {
    char h = s.charAt(i);
    //process h
}

Comments

2

The simplest loop through a string one character at a time would simply use the length of the string to handle this. For example:

if (inputString != null) {
  for (int i=0; i < imputString.length(); i++) {
     theChar = inputString.charAt(i);
     ...
  }
}

Comments

-1
  1. Use a for loop to iterate through all characters in the String until you get to String.length()
  2. Use a foreach loop to iterate through all characters that are returned from String.getBytes()

6 Comments

Seems like a senseless reason to throw an Ecxception. Why not just iterate over the each element after determining the length of the string.
Edited to offer options.
Remove the try/catch approach and I'll remove my downvote. You should not use Exceptions in this scenario.
I will remove, however it is a valid demonstration of technique, even if not for this exact example.
It is a possible approach, but it's a bad option here. We shouldn't teach the newb bad habits ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.