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 7 of 7

Thread: For Loop - need a hint for what to do next

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default For Loop - need a hint for what to do next

    Basically what i need to do is create a program which asks the user to enter their name which then displays the character length of the name which then converts the name into uppercase letter and finally print the name with the first character taken away then the first and second character taken away and so forth. I have been asked to do this using a loop and so far i'm certain a for loop is the ideal choice but i really am not sure how to implement the characters of the word into the loop, this is what i've managed to do so far:
    import java.util.Scanner;
    public class NameLength
    {
    	public static void main (String[]args)
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter your name:");
    		String name = scan.nextLine();
    		System.out.println("Name Entered: " + name);
    		int length = name.length();
    		System.out.println("Your name contains " + length + " characters");
    		String capitals = name.toUpperCase();
    		System.out.println("Your name in uppercase: " + capitals);
    		int length2 = capitals.length();
    		System.out.println("Your name contains " + length2 + " characters");
    		char firstLetter = name.charAt(0);
    		for 
      }
    }

    I've looked through all the lecture notes for loops but just can't seem to figure it out, any hints will be much appreciated


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: For Loop - need a hint for what to do next

    Hi.

    You are off to a very good start - let's go with the for-loop. A for-loop consists of three parts: initialization, termination, and incrementation. For example, I choose these three criterions for this loop:

    1. Increase some counter by 1 (incrementation)
    2. Start the counter at 0 (initialization)
    3. End the loop when the counter gets to 10 (termination)

    Hence, I can translate these three criterions to:
    for (int counter = 0; counter < 10; counter++) { // body of for-loop }

    If I write this for-loop in English, it would read: start the counter at 0; if counter is less than 10, execute the stuff in the body; increase the counter by 1 for every iteration. Once counter == 10, this condition is false: counter < 10, correct? 10 is not less than 10, so the for-loop stops. Let's do an example run..

    iteration 1:
    counter == 0
    Is this less than 10? Yes, so execute the body of the for-loop and increment the counter by 1.

    iteration 2:
    counter == 1
    Is this less than 10? Yes, so execute the body of the for-loop and increment the counter by 1.

    iteration 3:
    counter == 2
    Is this less than 10? Yes, so execute the body of the for-loop and increment the counter by 1.

    iteration 4:
    counter == 3
    Is this less than 10? Yes, so execute the body of the for-loop and increment the counter by 1.

    .
    .
    .

    iteration 11:
    counter == 10
    Is this less than 10? No, so end the for-loop. DONE!
    To attempt this problem, write down the output. Let's say you have the name TOMMY. Ultimately, you want:

    OMMY
    MMY
    MY
    Y
    Now that you have an idea how the for-loop works, what can you do in the "body of for-loop" to get the desired output?

  3. The Following User Says Thank You to vluong For This Useful Post:

    tufaylahmed (November 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: For Loop - need a hint for what to do next

    hi, thanks for the detailed response, from this i've worked out that this may work for the for loop as so there are enough repeat for the whole name:

    for (int i = 1; i < length; i++)

    but i still have no idea exactly what to put in the body, i mean it's printing the name but i'm not sure how to remove the first character then the first and second and so forth within the body without making a complete mess of it :/

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: For Loop - need a hint for what to do next

    Okay, what you have is perfect. Next hint: java substring.

  6. The Following User Says Thank You to vluong For This Useful Post:

    tufaylahmed (November 19th, 2012)

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: For Loop - need a hint for what to do next

    Done a bit more digging and come up with this:

    import java.util.Scanner;
    public class NameLength
    {
    	public static void main (String[]args)
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter your name:");
    		String name = scan.nextLine();
    		System.out.println("Name Entered: " + name);
    		int length = name.length();
    		System.out.println("Your name contains " + length + " characters");
    		String capitals = name.toUpperCase();
    		System.out.println("Your name in uppercase: " + capitals);
    		int length2 = capitals.length();
    		System.out.println("Your name contains " + length2 + " characters");
    		char firstLetter = name.charAt(0);
    		for (int i = 0; i < length; i++)
    		{
    			name += name.charAt(i);
    			System.out.println(name);
    		}
      }
    }

    This compiles perfectly although the problem is that this adds the character rather than taking it away for example

    TOMMY
    TOMMYT
    TOMMYTO
    TOMMYTOM
    TOMMYTOMM
    TOMMYTOMMY

    But when i try to change the operator to "-=" instead i get a java error saying
    19: error: bad operand types for binary operator '-'
    name -= name.charAt(i);
    ^
    first type: String
    second type: char
    i'm glad i made some sort of progress but not sure how to fix this

  8. #6
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: For Loop - need a hint for what to do next

    The "+" operator is an overloaded operator. This means that you can use it for adding integers an concatenating strings. You cannot use the "-" in the same way to remove characters, unfortunately.

    Good progress so far. As suggested, take a look into Java substring. This will make things a lot easier. If you decide to continue with string manipulation, notice that you are able to generate, nonetheless:
    T
    TO
    TOM
    TOMM
    TOMMY

    What you are able to generate is the complement of what you need:
    T [OMMY]
    TO [MMY]
    TOM [MY]
    TOMM [Y]

  9. The Following User Says Thank You to vluong For This Useful Post:

    tufaylahmed (November 19th, 2012)

  10. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: For Loop - need a hint for what to do next

    Ah! yes figured it out, working perfectly now, thanks for all the help!

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. Java application Hint
    By Ahmedz in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2012, 12:46 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM