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:
Code :
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
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:
Code :
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..
Quote:
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:
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?
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:
Code :
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 :/
Re: For Loop - need a hint for what to do next
Okay, what you have is perfect. Next hint: java substring.
Re: For Loop - need a hint for what to do next
Done a bit more digging and come up with this:
Code :
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
Quote:
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
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]
Re: For Loop - need a hint for what to do next
Ah! yes figured it out, working perfectly now, thanks for all the help! :D