Re: Strings and Characters
Quote:
The displayString() method should iterate over the String that is to be
displayed
Have you read the String class API documentation? The String class has methods to get at the individual characters in the String. You could write a loop to iterate over the String and get the char one by one.
Where is displayCharacter() displaying the argument it receives?
There is some disagreement between your documentation and your code:
lights.displayCharacter("V"); // here the arg is a String not a char.
chars are delimited by '
Quote:
displayCharacter() which takes a single argument of type char
Re: Strings and Characters
The displayCharacter() is displaying the argument it receives in an Array.
Re: Strings and Characters
Hi Norm , how could I write a loop to iterate over the String and get the char one by one?
Re: Strings and Characters
Ok. Normally I think of "display" as something that a human can see. Putting a value in an array is differeent than displaying it.
You "display" a String by using System.out.println to a console or by putting it in a GUI component like a text field.
Re: Strings and Characters
Quote:
could I write a loop to iterate over the String and get the char one by one?
You do it pretty much just like you asked:
write a loop -> See for() loop for example
in the loop get the char from the String one by one -> Read the String class API doc for the method
Re: Strings and Characters
for (char aChar = 0; char < 4; char++); something like this for HELLO ?
Re: Strings and Characters
CORRECTION for (char aChar = 0; achar < 4; achar++); something like this for HELLO ?
Re: Strings and Characters
Not quite.
You should use the length of the String to control the number of loops. Don't hard code a 4.
You've mixed the data type and the variable name: char aChar = 0; char < 4; char++
char is a data type, aChar is a variable name
What is the variable aChar being used for inside of the loop? Normally loop control variables are int.
Re: Strings and Characters
Sorry for (int i = 0; i < string.length; i++) ?
Re: Strings and Characters
Time to use the compiler and see what it thinks.
Re: Strings and Characters
I dont quite know how to incorporate the loop statement with everything else - sorry just not syntactically or semantically there!
Re: Strings and Characters
Just start with the loop.
Then add the other stuff one thing at a time.
There are hundreds of pieces of code on this forum and thousands on the internet.
Search here or Google for samples.
Also read the Java Tutorial: http://download.oracle.com/docs/cd/E...vase/tutorial/