• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Making spaces in between words

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic