i am trying to print the same string multiple times...
not sure how i could do this in java...


i used to be able to just do this in python... but it doesn't seem to work in java:

print "*" * 10
and it should print it 10 times
(not sure actually... have not programmed in python for a while... so i could be wrong as well...)

but the main question is... how do i print the same string multiple times with respect to an integer variable?

please help!
A For loop?
souvik1997 wrote:
A For loop?

well.. i am trying to a void loops...
since i am trying to print something like this:

[ 0.0 - 1.0) | ***********

and the number of * is specified by a variable
Yeah, the internet says use a StringBuilder and a loop.
Edit: I would assume that you make it a method for easy invocation.
calcdude84se wrote:
Yeah, the internet says use a StringBuilder and a loop.

what? Shock
so if i use loops i will need like 10 loops since i have 10 print statements...

and the beginning of each one of the mis static... while they are suppose to be in the same line...

so how would i do that? Shock
You can pretty easily write this function:

Code:
public static String repeatString(String s, int n) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < n; i++) {
      sb.append(s);
   }
   
   return sb.toString();
}
Now whenever you want a string of n strings, s, you do:

Code:
repeatString(s, n);
Someone more familiar with java can tell me if this is the right way or not, but it works, and I don't see anything terrible about it.
HEY!
look what i found!

though i keep getting errors...

whats the corect way to use this?
print(char ch, int n)
Print a char N times

found here:
http://www.jarvana.com/jarvana/view/com/hp/hpl/jena/arq/2.8.5/arq-2.8.5-javadoc.jar%21/org/openjena/atlas/io/IndentedWriter.html

how do i import it and how do i use it? :/
aka what do i type exactly to get it to print that way?
EDIT:
oh... i didn;t see your post...
but java lang seems to have that already!
not sure how to use it though... a little help please!
i tried this:
Object.println(ch, row9);
and this:
System.out.println(ch, row9);

but neither seem to work
and of course i imported java.lang.Object;
bump
Please don't bump a post after only 43 minutes.
merthsoft wrote:
Please don't bump a post after only 43 minutes.

please dont get my hops up with stuff like this... -.-

i am in quite a rush...

thanks for nothing... Crying
...

Merth's solution is easy to implement and works. If you want something potentially more efficient, there are a variety of other helpers in the Apache commons libraries, although that then requires you have those packages installed in addition to the base Java libraries.

TL;DR version:
Use either org.apache.commons.lang.StringUtils.repeat(String, int) or Merth's solution.

Of course, judging from your apparent lack of grasp of some fundamental concepts, I suppose you need this spoon-fed to you.. I'm not about to do that, though.
The Tari++; Merth++. Being in a rush is no reason to ignore forum etiquette, especially when a partial solution has already been offered to you. And "thanks for nothing" is pretty passive-aggressive. Rolling Eyes

Edit: I also find it entertaining that you linked to a page that clearly defines some custom Java class, and are trying to magically have System.out know about what that indentation class can do. As Tari said, you clearly have paid no attention in your Java class, and trying to use us to do your homework right before your homework is due, then yelling at us when we don't spell everything out immediately for you, is some pretty despicable work.
KermMartian wrote:
The Tari++; Merth++. Being in a rush is no reason to ignore forum etiquette, especially when a partial solution has already been offered to you. And "thanks for nothing" is pretty passive-aggressive. Rolling Eyes

Edit: I also find it entertaining that you linked to a page that clearly defines some custom Java class, and are trying to magically have System.out know about what that indentation class can do. As Tari said, you clearly have paid no attention in your Java class, and trying to use us to do your homework right before your homework is due, then yelling at us when we don't spell everything out immediately for you, is some pretty despicable work.

I am too lazy to make up my own post, so instead I quote others and then don't say (much of) anything new.

(Moved to Programming/General)
merthsoft wrote:
You can pretty easily write this function:

Code:
public static String repeatString(String s, int n) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < n; i++) {
      sb.append(s);
   }
   
   return sb.toString();
}
Now whenever you want a string of n strings, s, you do:

Code:
repeatString(s, n);
Someone more familiar with java can tell me if this is the right way or not, but it works, and I don't see anything terrible about it.


Easy optimization on that:


Code:
public static String repeatString(String s, int n) {
   StringBuilder sb = new StringBuilder(s.length() * n);
   for (int i = 0; i < n; i++)
      sb.append(s);
   return sb.toString();
}


Only one memory allocation then Smile
Ah, of course, excellent point. I was posting on my phone, and didn't have the javadocs handy, so I wasn't sure if there was a constructor that took an int. I suppose it would be silly for there not to be, but I didn't want to lead him down the wrong path in case there wasn't.

Out of curiosity, do you think this is indeed what the Apache Commons library does, or does it go about it a different/better way?
merthsoft wrote:
Out of curiosity, do you think this is indeed what the Apache Commons library does, or does it go about it a different/better way?


No clue, take a look at the source and find out.
Good call. For the record, that's basically what it does, but if the length is one or two, it uses char arrays instead, which makes sense:

Code:
public static String repeat(String str, int repeat) {
   // Performance tuned for 2.0 (JDK1.4)

   if (str == null) {
      return null;
   }
   if (repeat <= 0) {
      return EMPTY;
   }
   int inputLength = str.length();
   if (repeat == 1 || inputLength == 0) {
      return str;
   }
   if (inputLength == 1 && repeat <= PAD_LIMIT) {
      return padding(repeat, str.charAt(0));
   }

   int outputLength = inputLength * repeat;
   switch (inputLength) {
      case 1 :
         char ch = str.charAt(0);
         char[] output1 = new char[outputLength];
         for (int i = repeat - 1; i >= 0; i--) {
            output1[i] = ch;
         }
         return new String(output1);
      case 2 :
         char ch0 = str.charAt(0);
         char ch1 = str.charAt(1);
         char[] output2 = new char[outputLength];
         for (int i = repeat * 2 - 2; i >= 0; i--, i--) {
            output2[i] = ch0;
            output2[i + 1] = ch1;
         }
         return new String(output2);
      default :
         StringBuilder buf = new StringBuilder(outputLength);
         for (int i = 0; i < repeat; i++) {
            buf.append(str);
         }
         return buf.toString();
   }
}
There are a few things I'm curious about, though. Why only if it's length one or two do they use an array? Did they do performance testing and decide those were optimal, or did they just not want to write ten cases? I imagine it was some balance between performance and code-succinctness. I also am curious as to why they fill the arrays backward. I never thought there'd be a performance difference between filling arrays backward vs. forward, so maybe it's just a choice they made? I'll look into it and report back, unless someone knows off the top of their head.
merthsoft wrote:
I also am curious as to why they fill the arrays backward. I never thought there'd be a performance difference between filling arrays backward vs. forward, so maybe it's just a choice they made?

I'm guessing a bit here, but I believe reversing for loops like that isn't an uncommon compiler optimization, presumably because it's usually cheaper (by an instruction or two) to compare against zero than an arbitrary value, so they probably just did that optimization explicitly.

Either that or whoever wrote it just likes to iterate from the end.
merthsoft wrote:
There are a few things I'm curious about, though. Why only if it's length one or two do they use an array? Did they do performance testing and decide those were optimal, or did they just not want to write ten cases?


Why stop at ten? What about 100? 1000? I think you are confusing inputLength with repeat while reading that code. Basically what they did was have a simple optimization for the case of repeating one or two characters, and then a general solution to the problem.

Quote:
I imagine it was some balance between performance and code-succinctness. I also am curious as to why they fill the arrays backward. I never thought there'd be a performance difference between filling arrays backward vs. forward, so maybe it's just a choice they made? I'll look into it and report back, unless someone knows off the top of their head.


Whoever wrote it felt like going backwards - no performance difference.
Kllrnohj wrote:
merthsoft wrote:
There are a few things I'm curious about, though. Why only if it's length one or two do they use an array? Did they do performance testing and decide those were optimal, or did they just not want to write ten cases?


Why stop at ten? What about 100? 1000? I think you are confusing inputLength with repeat while reading that code. Basically what they did was have a simple optimization for the case of repeating one or two characters, and then a general solution to the problem.
Right, so why have the two specific cases and then the general? Why not have the simple optimization for repeating three characters? Or does the optimization break down after that?

Quote:
Quote:
I imagine it was some balance between performance and code-succinctness. I also am curious as to why they fill the arrays backward. I never thought there'd be a performance difference between filling arrays backward vs. forward, so maybe it's just a choice they made? I'll look into it and report back, unless someone knows off the top of their head.


Whoever wrote it felt like going backwards - no performance difference.
What about what Tari said about the comparison to zero? I googled around and saw that reasoning come up a little bit, but I'm not familiar enough with how all that would be implemented to know.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement