Results 1 to 7 of 7
  1. #1
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Problem with printing every other letter. Please help

    Let me first give you the requirements and then I'll show you my code:

    In the main method of this class file, create a string called sentence and assign it a word or sentence of your choice. The rest of the assignment involves working with the contents of this sentence. You should test and ensure that your program works with sentences of different lengths and characters (letters/numbers/special characters).
    We will use this sentence as a password starting phrase to automatically create a new password as follows.
    <10>Task 1. Create a new string called passwd formed by concatenating every alternate non-space character in sentence starting with the first. To do this, you need to a loop to go through the string sentence and retrieve characters from alternate index positions.

    String sentence = "B@n@n@ nut";
    System.out.println("The starting sentence is:"+sentence);
    for(int i=0; i<sentence.length(); i+=2){
    System.out.print(sentence.charAt(i));

    So this prints out to:....
    The starting sentence is: B@n@n@ nut
    Bnn u


    I'm not sure if i'm doing it right with the code I am using. I also do not know how to get "The password is:" before the "Bnn u" part. If I put System.out.print("The password is:"+sentence.charAt(i)) then i get...

    The password is: B The password is:n The password is:n The password is: The password is:u

    I only want it to show "The password is:" once. If anyone could go over the requirements and tell me how to alter my code to make it work I would greatly appreciate it!! Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Problem with printing every other letter. Please help

    You need to build the new String up before printing it to the console.
    So your loop should (as it says in the task) be concatenating alternate letters together, in another String.
    Please do not ask for code as refusal often offends.

  3. #3
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Problem with printing every other letter. Please help

    Thanks for the reply. How do I build the string up? I'm very new to java.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Problem with printing every other letter. Please help

    See the section in this tutorial on concatenating Strings.
    Please do not ask for code as refusal often offends.

  5. #5
    codec is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Problem with printing every other letter. Please help

    Quote Originally Posted by Tolls View Post
    See the section in this tutorial on concatenating Strings.
    I've been looking at that but am not sure if what I need is an array...

  6. #6
    jp_armystrong is offline Member
    Join Date
    Jan 2013
    Location
    Stockton, CA
    Posts
    2
    Rep Power
    0

    Default Re: Problem with printing every other letter. Please help

    you always need to type in System.out.print(sentence.charAt(i)); in different line. Because the plus sign treats it as a single sentence so reiterates it everytime.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Problem with printing every other letter. Please help

    Quote Originally Posted by codec View Post
    I've been looking at that but am not sure if what I need is an array...
    No you don't.
    That page describes concatenation.
    There is a section halfway down titled "Concatenating Strings".
    Nowhere in that section does it talk about arrays.

    It mentions the String.append() method and using '+', the latter being the most common way of doing it.

    Now, what do you not understand about that?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Capitalizing a letter - semantic problem
    By Humphrey Bogart in forum New To Java
    Replies: 2
    Last Post: 11-08-2012, 06:41 PM
  2. Letter counter homework problem
    By Djgnl in forum New To Java
    Replies: 15
    Last Post: 09-24-2011, 02:37 AM
  3. Problem in printing
    By justbeller in forum Java 2D
    Replies: 0
    Last Post: 04-26-2011, 02:48 PM
  4. Printing Out Every Other Letter
    By Agent in forum New To Java
    Replies: 4
    Last Post: 11-20-2008, 12:43 AM
  5. Problem after Printing GUI.
    By coldblood22 in forum AWT / Swing
    Replies: 1
    Last Post: 04-05-2008, 02:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •