0

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

2
  • 1
    probably better to do your own homework. Commented Nov 12, 2012 at 20:04
  • for (int n = word.length(); n >= 0; --n) { System.out.println(word); } Commented Nov 12, 2012 at 20:17

3 Answers 3

3

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); } 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.