April 30th, 2013, 12:18 PM
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.
It okay guys I figured it out I put it like this:
public static void ListCharactersInName(String name)
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)
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.
With that for loop, you can declare the counter inside it rather than outside
public static void listCharactersInName(String name)
// Your code goes here.
eg. Rather than
You can say simply
for (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
for (int counter = 0; counter < length.......)
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'.
counter = counter +1;
You can't take more than one away using these shorthands though (counter--- doesn't mean counter = counter-2 )
counter = counter-1;
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 06:51 PM.
Thanks for the help PS: I am 13 so thanks