charAt()

I'm new to programming and am trying to figure out why the following program will not produce the desired results. It's supposed to read a string, then print each character of the string on a new line.

I've tried do loops and for loops, changed the 'letter' variable to a char, and everything else I could think ofany ideas? Thanks.

import cs1.Keyboard;

publicclass OneChar{

publicstaticvoid main (String[] args){

String input;

char letter = 0;

System.out.print("Enter a string: ");

input = Keyboard.readString();

while (input.charAt(letter) <= input.length())

{

System.out.println(letter);

letter++;

}

System.out.println("Done");

}

}

[1323 byte] By [mts767] at [2007-9-26 3:10:14]
# 1
What's "csl.Keyboard"?Rich
rmiller1985 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 2
What results are you getting? What error messages are you receiving?
pendalama at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 3
a class used to accept user input.
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 4
No error messages. The only result is "Done" printed to the screen.
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 5
try System.out.println(input);before entering your loop to see if the String really contains data.
pendalama at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 6
If csl.Keyboard is a class used for reading input, it would be helpful to see that class in order to figure out the code. At the very least, you should print out what you read in using that class to verify that it contains what you think it contains.Rich
rmiller1985 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 7
Yes, this line results in the String I enter to be printed to the screen.
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 8
D'oh!You don't want to print out "letter" in the loop, you want to print out "input.charAt(letter)" in the loop.Rich
rmiller1985 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 9
Still doesn't print one character per linein fact, nothing from the loop prints.
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 10

Second d'oh!

In your "while" statement, you don't want to compare the character at the current position to the length of the string, you want to compare an int that is set to the index of the current position. In other words, instead of:

while (input.charAt(letter) <= input.length())

you want:

while (letter <= input.length())

I think you can use a char, but I don't know why you'd use a char instead of an int.

Rich

rmiller1985 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 11
try this for you loop ...while (letter < input.length()){System.out.println(input.charAt(letter));letter++;}
Judd Smith at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 12
That's it--the program now prints a string one character per line. Thanks.However, when I run the program (after it prints the results) it also issues a "StringIndexOutOfBoundsException".
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 13

Did you forget to remove the <= and put a < instead?

Remember, indexes are 0 bound, so if your string has 12 characters, they will be numbered 0 to 11. With <=, you would be checking character #12, which is not valid, hence the "index out of bounds" error.

(of course, Java is smart enough to tell you that your string is from 0 to 11 and 12 is too high. In C/C++, you would just have gotten garbage, because character #12 would be part of another variable.)

btw, you should use int letter instead of char letter. letter is an index into your string, therefore it's a number not a character itself.

Good luck

Skylark13 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 14
Got it. Thanks for your help.
mts767 at 2007-6-29 11:16:37 > top of Java-index,Archived Forums,Java Programming...
# 15

import cs1.Keyboard;

public class OneChar {

public static void main (String[] args) {

String input;

System.out.print("Enter a string: ");

input = Keyboard.readString();

for( int i=0; i<input.length(); i++ )

System.out.println( input.charAt( i ) );

>

adaptardar at 2007-7-1 3:26:02 > top of Java-index,Archived Forums,Java Programming...
# 16
Do you go to GVSU? Just thought I'd ask because I reconized the Keyboard Class we used there.
lestat14 at 2007-7-1 3:26:02 > top of Java-index,Archived Forums,Java Programming...
# 17
What's GVSU? I'm on and off at DePaul.
mts767 at 2007-7-1 3:26:02 > top of Java-index,Archived Forums,Java Programming...
# 18
Grand Valley State University in Michigan.
lestat14 at 2007-7-1 3:26:02 > top of Java-index,Archived Forums,Java Programming...