Reputation: 59
... using for loops in Java. For example, hello would be printed five times.
Here is my code so far:
import java.util.Scanner;
class Words {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter one word of your choice");
String word = scan.nextLine();
System.out.println(word);
int length = word.length();
System.out.println(length);
for (length < word; length++) {
System.out.println(word);
}
}
}
We have to use the scanner package. Sorry if this is really basic but I'm just starting out and can't find an answer anywhere!
Upvotes: 0
Views: 2098
Reputation: 155
I do C# programing, but this is similar. Just user something like this:
string word = "hello";
int times = word.Length;
for(int i = 0; i < times; i++)
{
System.out.println(word); // Java statment from your code
}
Hope it helps ...
Upvotes: 1
Reputation: 28579
for(int x = 0; x < word.length; x++){
System.out.println(word);
}
Upvotes: 0
Reputation: 34367
You just need to write a loop which runs for 0 to the length of the word e.g. below:
int length = word.length();
for (int i = 0; i < length; i++) {
System.out.println(word);
}
Upvotes: 3