#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Need help dont know what to do.


    Hi guys I really need help on java writing out each letter of my name on a different line using a for loop is there any chance anybody could help me. PS if u hadn't noticed im new to java programming and want to do it in future but im 13 thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    It okay guys I figured it out I put it like this:


    public static void ListCharactersInName(String name)
    {



    int counter;
    char lower;
    int length;
    String uppercase;
    char upper;
    uppercase = name.toUpperCase();
    length = name.length();



    for(counter = 0;counter<length;counter = counter + 1)
    {
    lower = name.charAt(counter);
    upper = uppercase.charAt(counter);


    if (upper == lower)
    {
    System.out.println(lower);
    }
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    Glad you got it sorted... But in the interest of your learning, let me explain a few optimisations you could make....

    Firstly, by convention (there are no rules & no-one forcing you to do this, but just because that's the way we all tend to do things) methods are written with the first word uncapitalised.

    eg.
    Code:
    public static void listCharactersInName(String name)
    {
        // Your code goes here.
    }
    With that for loop, you can declare the counter inside it rather than outside

    eg. Rather than
    Code:
    int counter;
    for (counter = 0; counter< length..........)
    You can say simply
    Code:
    for (int counter = 0; counter < length.......)
    Also, when you want to add one (increment) to an integer, there's a nice shorthand way of doing it. The following two lines are identical in their output. Chose whichever you understand best

    Code:
    counter = counter +1;
    counter++;
    Note: You can also take one away (decrement) using a similar approach. The following 2 lines are again identical to each other. They take one away from whatever counter 'used to be'.

    Code:
    counter = counter-1;
    counter--;
    You can't take more than one away using these shorthands though (counter--- doesn't mean counter = counter-2 )

    You also don't need the if statement because upper & lower are always equal. Simply do the System.out.println statement.

    ps. If you really are 13 I am really really impressed! You're going to be good!
    Last edited by doa24uk; May 1st, 2013 at 05:51 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Thanks for the help PS: I am 13 so thanks

IMN logo majestic logo threadwatch logo seochat tools logo