Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

display a string X times?

Thread view: 
guitarromantic@gmail.com - 31 Oct 2005 20:14 GMT
I'm being asked to represent an integer score value in asterisks
instead of just as a digit. I figured (as a newbie) I could just define
a string 'star' (as "*") and just call it like score * star..
apparently not. How can I do this? My other idea is using a while loop,
but I think this could be a bit unwieldy, for the scale of the program
it's for.

Matt
Oliver Wong - 31 Oct 2005 20:27 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
> a string 'star' (as "*") and just call it like score * star..
> apparently not. How can I do this? My other idea is using a while loop,
> but I think this could be a bit unwieldy, for the scale of the program
> it's for.

   I'd use a for-loop if I were you. But a while-loop would probably do
just as well (subtle pun).

   - Oliver
guitarromantic@gmail.com - 31 Oct 2005 20:59 GMT
Thankyou, you too Carl.. I actually meant a 'for' loop, not while, I
just got mixed up. I've got this working exactly as I needed it to now,
anyway, so thanks again!
Carl - 31 Oct 2005 20:28 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
[quoted text clipped - 4 lines]
>
> Matt

I would use a 'for' loop.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

Carl.
Thomas Schodt - 01 Nov 2005 10:33 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
> a string 'star' (as "*") and just call it like score * star..
> apparently not. How can I do this? My other idea is using a while loop,
> but I think this could be a bit unwieldy, for the scale of the program
> it's for.

As you say 'digit' I assume the range is [0;9], so

public static final String STAR9 = "*********";
...
  STAR9.substring( /*for you to figure out*/ );

As there are only 10 different scores, you could cache them in an array
so you don't create a new String object every time.

If you have larger numbers to deal with
use a StringBuffer to "double up"

public static final StringBuffer STARS = new StringBuffer("****");
...
  while (STARS.length()<score) STARS.append(STARS);
  STARS.substring( /*for you to figure out*/ );


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.