Solved

Creating a white space

Posted on 2002-06-26
5
174 Views
Last Modified: 2010-03-31
Is there an existing class in Java wherein I can use just say create 50 spaces and start displaying a string in the text Area?

I don't like to create a method that just loop and create a space with append the string I want.
0
Comment
Question by:bubwit
5 Comments
 
LVL 5

Accepted Solution

by:
Calron earned 20 total points
ID: 7112844
You probably need this to set the initial size of the TextArea, instead of just filling in spaces, you can also use the methods: setColumns(int), be sure to read the API doc of the setColumns(int) and getColumnWidth() as setting the number of columns is not the same as allowing for n characters.

If you still want to use spaces, there is no ready made class, I would do the loop, though make sure you don't loop on a String (way too slow and creates a ton of temporary string objects), use StringBuffer:

StringBuffer s = new StringBuffer();
for (int i = 0; i < 50; i++) {
  s.append(" ");
}
String finalString = s.toString();


of course you can make this more efficient by adding 2 or more spaces at a time, its just supposed to be an example
0
 
LVL 92

Expert Comment

by:objects
ID: 7112929
This should be a bit faster:

char[] spaces = new char[50];
Arrays.fill(spaces, ' ');
String s = new String(spaces);
0
 

Expert Comment

by:udaykumar22
ID: 7115965
Hi,

I think this would be the fastest ;-)

String Str = new String("                                                 ");

Regards,
Uday.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8893423
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to Calron

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question