• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making spaces in between words

 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???



Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34859
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,
Welcome to JavaRanch!

So you are trying to merge two or more spaces into one space? Post what you have so far.
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
System.exit(0);
}


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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
John Mallavalli
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic