-1

I have a variable:

String title = "titleHere";

How would I print the name out three times instead of writing

System.out.println(title);
System.out.println(title);
System.out.println(title);

or using a for loop?

For example, Ruby lets you write title * 3 and Perl lets you write title x 3, so does Java have this too?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
jdc987
  • 743
  • 3
  • 10
  • 20
  • ... thanks ...I know how to use the for loop I was just wondering if there was a way to do it without it like System.out.println(title*3), but I guess not. – jdc987 Nov 05 '13 at 03:18
  • Totally checked the other questions you've asked, and you aren't this novice... If this were Facebook, I would ask if you've been "hacked..." – Tonithy Nov 05 '13 at 03:19
  • Please read the basics of programming. then you would not came through such doubts. – Balaji Dhanasekar Nov 05 '13 at 03:19
  • 2
    No doubts, just looking for an alternative way. Seeing as theres always more than way to do something, I figured I'd ask. – jdc987 Nov 05 '13 at 03:20
  • 3
    I see what you were asking for... That would have to have been written into the Java API, it isn't, but check my answer... – Tonithy Nov 05 '13 at 03:26
  • Closely related to http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – Raedwald Jan 05 '15 at 08:01

5 Answers5

3

You can make use of for loop. Just change init condition (i=0) and loop break check (i<3) as per your need.

 String title;
    title = "titleHere";


for (int =0;i<3;i++){
 System.out.println(title);

}
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
1

add a for loop like

for(int i = 0; i < no_of_times; i++)
{`
    System.out.println(title);
}`
tausun
  • 2,154
  • 2
  • 24
  • 36
1

Write a [static] utility method that takes a string and an int.

public static void multiPrint(String message, int printCount) {
   for (int i = 0; i < printCount; i++) {
       System.out.println(message);
   }
}

I'd probably put it in a separate class and call that whenever you need the functionality...

Tonithy
  • 2,300
  • 1
  • 20
  • 20
  • For my purposes, this was exactly what I needed. I didn't think to put it in a method. Thanks man. – jdc987 Nov 05 '13 at 03:34
  • @jdc987 Awesome, but why did you accept The Dark Knight's answer over mine. I feel like Harvey Dent... I "got the girl," but I'm dead. I guess Batman always wins in the end... – Tonithy Nov 05 '13 at 03:37
  • 1
    @jdc987 Also, there is a reason methods like this don't exist in API, they tend to be niche cases and simple to implement for the individual programmers that need them. I usually make a 'Utilities' class filled with tons of static methods to do the obscure things that I need depending upon the project. – Tonithy Nov 05 '13 at 03:40
  • @Tonithy That is exactly what I do. In fact, I create a bunch of classes with static methods, grouping like methods together (for easier reference when I want to change something). Then, I have my `Utils` class extend one which extends another, etc (creating an "extends" chain) – Justin Nov 05 '13 at 04:39
0

Well if you're looking for another alternative, you could also use a while loop:

String title = "titleHere";

int i = 0;
while (i < 3) {
    System.out.println(title);
    i++;
}
ashatte
  • 5,442
  • 8
  • 39
  • 50
0

Use a For Loop

String title;
title = "titleHere";
for(int i = 0; i < no_of_times; i++)
{
    System.out.println(title);
}
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106