Solved

Creating a white space

Posted on 2002-06-26
5
169 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
Comment Utility
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
Comment Utility
This should be a bit faster:

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

Expert Comment

by:udaykumar22
Comment Utility
Hi,

I think this would be the fastest ;-)

String Str = new String("                                                 ");

Regards,
Uday.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now