Michael Delahorne
Michael Delahorne

Reputation: 59

I need to print a word equal to the amount of characters in the word

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

Answers (3)

josefpospisil0
josefpospisil0

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

Matt Clark
Matt Clark

Reputation: 28579

for(int x = 0; x < word.length; x++){
    System.out.println(word);
}

Upvotes: 0

Yogendra Singh
Yogendra Singh

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

Related Questions