The Artima Developer Community
Sponsored Link

Java Answers Forum
Printing a String of a defined number of

1 reply on 1 page. Most recent reply: Jul 28, 2003 1:59 PM by K. Hodel

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Printing a String of a defined number of Posted: Jul 28, 2003 10:14 AM
Reply to this message Reply
Advertisement
As you can see I am working on a HangManGame for school. I am trying to print a string with a certain number of astreiks. My current method involves me using a for loop. This however, I realize is not the best way to do it. I need to use the substring method later on on these asteriks. But the number of asteriks keeps changing because it all depends on what secret word you type in.

Can anyone please help me write a code that will print a certain number of characters based on a .length variable.

Thankyou


K. Hodel

Posts: 8
Nickname: hodeka104
Registered: Jul, 2003

Re: Printing a String of a defined number of Posted: Jul 28, 2003 1:59 PM
Reply to this message Reply
David's reply to your first post suggested using a StringBuffer. Here's one way to do that:
  int len = 10;
  StringBuffer stars = new StringBuffer();
  for ( int i = 0; i < len; ++i ) {
    stars.append( "*" );
  }

Then, if you want to print the string, do something like
  System.out.println( stars.toString() );

Using the StringBuffer also has the advantage of letting you easily change the string:
  stars.setCharAt( 5, "R" );
  System.out.println( stars.toString() );

Flat View: This topic has 1 reply on 1 page
Topic: Vector Problem Previous Topic   Next Topic Topic: Please help with string problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use