This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Making spaces in between words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making spaces in between words" Watch "Making spaces in between words" New topic

Making spaces in between words

Adam Favel

Joined: Feb 09, 2005
Posts: 22
i'm working on this program. You write in a string, and if there were more than one space in front of, in the middle or at the end of the string, the program will get rid of them, and also will put only one space in between characters. I can get rid of the spaces at the end and beginning, but i cannot figure out how to put spaces in between words. Can anyone help me???

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

Welcome to JavaRanch!

So you are trying to merge two or more spaces into one space? Post what you have so far.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Adam Favel

Joined: Feb 09, 2005
Posts: 22
import javax.swing.*;

public class Question3{
public static void main(String[] args){

String str;
String NewString;
int strLength;
int i;
char ch;

str = JOptionPane.showInputDialog("Enter a String");

while (str.equals("quit") == false) {
NewString = "";

strLength = str.length();

for(i = 0; i <= strLength - 1; i++){
ch = str.charAt(i);
if(ch == ' ') {

else {
NewString = NewString + ch;

System.out.println("The Original String Was:\""+str+"\"");
System.out.println("The Fixed Up String is:\""+NewString+"\"");

str = JOptionPane.showInputDialog("Enter Another String");
System.out.println("End of Processing");

The Point of the program is to enter a string, usually a really messed up one with too many spaces in front and behind, and the program is supposed to correct this, and evenly space out the words, and get rid of the spaces in front and behind.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Adam -

Welcome to JavaRanch. If you put code excerpts in CODE UBB tags, it will look like this and more people will tend to read your posts and reply...

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
John Mallavalli
Ranch Hand

Joined: Apr 24, 2002
Posts: 46
Hi Adam,

You can do achieve this in many ways and one way can be as follows:
As you traverse through each character of the string just set a flag which holds whether the last character that you had passed is a space or not. So if the flag is set it means that the previous character that you had passed is a space and hence you need to just skip that space and check for the next character. Proceed using the same logic till you encounter the end of the string. Hope this is clear. You can refer to the below code for reference.

Hope this helps!
- John

Do not let what you cannot do interfere with what you can do !!<br /> <br />SCJP & SCWCD 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A shorter and more readable, but not necessarily more efficient, chunk of code might split the string into words, then concatenate the words back toegether with one space between them. Look at the APIs for String.split() or StringTokenizer or Scanner and see what you think.

My old favorite language, REXX, has a built-in function that puts a specified number of blanks between words. For some reason I used to use zero blanks fairly often.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You may also want to look into StringBuffer (or StringBuilder, if you are using Java 1.5). These classes are used for building String objects while minimizing the memory usage.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Making spaces in between words
It's not a secret anymore!