Write a method to print out a string "Name" multiple times

user705447 picture user705447 · Apr 13, 2011 · Viewed 96.8k times · Source

I am answering a question for an Intro to Programming class and cannot - after reading through my notes, Big Java book, and looking online - find out where to begin.

It seems as though it should be very simple, but I just need to get started. All of this is for Java and is being worked on in Eclipse.

The task is to take in a name ("Name") and a number (int x) and display the "Name" x number of times on one line, x-1 times on another line, and so on and so on until you display the name only once. It seems like it should be a reverse accumulator, but I'm having trouble starting my method. How do I begin? I know I can't multiply strings in Java like you can in python or other languages, but how can I print the "Name" x number of times without building an Array or inputing

System.out.println("name" + " " + "name" + " "...).

Any suggestions are appreciated. I am a novice at this. Thank you!

Answer

Andreas Dolk picture Andreas Dolk · Apr 13, 2011

You need a loop, a common feature of programming languages.

Have a close look at the java for loop.

Additional hint:

System.out.println("test") prints it's argument in a single line while System.out.print("test") doesn't add a CR/LF after test.