Home Tutorial Java Core Repeat word as many times as it has characters

 
 

Repeat word as many times as it has characters
Posted on: October 24, 2009 at 12:00 AM
Advertisement
This section illustrates you the repetition of same word as many times as it has characters in it.

Repeat word as many times as it has characters in Java

This section illustrates you the repetition of same word as many times as it has characters in it. To do this, we prompt the user to enter the word using the Scanner class. After finding the length of the word, we have created a for loop, it will display the entered word no of times its length.

Here is the code:

import java.util.*;
class RepeatWord{
  public static void main(String[] args)  {
  System.out.println("Enter word");
  Scanner input=new Scanner(System.in);
  String word=input.next();
  int len=word.length();
    System.out.println();
  for(int i=0;i<len;i++){
    System.out.println(word);
  }
  }
}

Output:

Enter word
Hello

Hello
Hello
Hello
Hello
Hello

Advertisement

Related Tags for Repeat word as many times as it has characters:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com