Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Strings and Characters

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strings and Characters

    Hello

    I would like to write a method called displayString() which takes a string as an argument and returns no result.


    I already have a working method I wrote called displayCharacter() which takes a single argument of type char called aCharacter.

    UpInLights is my class and when I create an instance of UpInLights:

    UpInLights lights = new UpInLights() and then execute the following: lights.displayCharacter("V");

    the letter V is displayed


    The method displayString() must use the displayCharacter() method within its method. The displayString() method should iterate over the String that is to be
    displayed. On each iteration the method should send a displayCharacter() message with the current element as its argument to the receiver.

    The result will be the following: lights.displayString("HELLO"); will display HELLO

    I am inexperienced and wouldn't know where to start so can't even post what I think is correct, but I am keen to learn and would love to know how this
    mini exercise can be achieved.

    Many thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Strings and Characters

    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 '

    displayCharacter() which takes a single argument of type char
    Last edited by Norm; July 3rd, 2011 at 02:33 PM.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    The displayCharacter() is displaying the argument it receives in an Array.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    Hi Norm , how could I write a loop to iterate over the String and get the char one by one?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Strings and Characters

    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

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    for (char aChar = 0; char < 4; char++); something like this for HELLO ?

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    CORRECTION for (char aChar = 0; achar < 4; achar++); something like this for HELLO ?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    Sorry for (int i = 0; i < string.length; i++) ?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Strings and Characters

    Time to use the compiler and see what it thinks.

  12. #12
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strings and Characters

    I dont quite know how to incorporate the loop statement with everything else - sorry just not syntactically or semantically there!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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/
    Last edited by Norm; July 3rd, 2011 at 03:33 PM. Reason: Where to get code samples

Similar Threads

  1. [SOLVED] characters per line in Eclipse
    By joon in forum Java IDEs
    Replies: 1
    Last Post: June 20th, 2011, 07:47 AM
  2. [SOLVED] Characters per line in Eclipse
    By joon in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2011, 04:41 PM
  3. [SOLVED] Four arrays 32 line characters from text
    By sketch_flygirl in forum Collections and Generics
    Replies: 7
    Last Post: March 31st, 2011, 10:33 PM
  4. finding unescaped XML characters
    By diptee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 18th, 2010, 04:51 AM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM