0

I have a String that I want to format in Java. Here is my code:


import java.util.*;
public class Test{

public static void main(String[] args){
  String a = "John       College of NY      NY  10286";
  a = removeSpaces(a);
  System.out.println(a);  
}

public static String removeSpaces(String s) {
  StringTokenizer st = new StringTokenizer(s," ",false);
  String t = "";
  while(st.hasMoreElements()){
     t =  t + ";" + st.nextElement();
  }
  return t;
}



When I run my code I get this back: (don't mind the semicolon in the front)
;John;College;of;NY;NY;10286



The output text should be separated by a semicolon in three parts like so, and it should preserve the 1 or 2 white spaces in the last two:

;John;College of NY;NY  10286;


I am not sure how to keep the the white space between "College" and "of" & "of" and "NY". Or the two white spaces between "NY" and "10286". How can I format the line?
unsignedShort
  • 93
  • 1
  • 3

4 Answers4

1

It's unclear from your question how your fields are meant to be delimited. If it's "3 or more spaces", (which I'm inferring from when you say "it should preserve the 1 or 2 white spaces") then you can use String.split with a regex:

StringBuilder sb = new StringBuilder();
for (String s : "John       College of NY      NY  10286".split("\\s{3,}")) {
  sb.append(';');
  sb.append(s);
}
return sb.toString();

This returns:

";John;College of NY;NY  10286"
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • String a = "John College of NY NY 10286"; System.out.println(a.replaceAll("\\s{3,}", ";")); – Kennet Dec 04 '10 at 22:07
0

Well, with string like that, there isn't really a good way, because you must have an explicit rule to separate substrings. Is the separator a whitespace, two whitespaces, any number of them... ?

Now, you could do it more easily if you had something like this:

 String a = "John       'College of NY'      'NY  10286'";

Then you could keep spaces within strings with quotes. Is this within your homework requirements?

If so, see this answer: Regex for splitting a string using space when not surrounded by single or double quotes

Community
  • 1
  • 1
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
0

i would first seperate the items in the right parts (with the length)

try {
    Pattern regex = Pattern.compile("(.{11})(.{19})(.{4})(.+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        // matched text: regexMatcher.group()
        // match start: regexMatcher.start()
        // match end: regexMatcher.end()
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

And after that i would trim the whitespace around

try {
    String resultString = subjectString.replaceAll("(?i)\\A\\s*(.*?)\\s*\\Z", "$1");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
} catch (IllegalArgumentException ex) {
    // Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
    // Non-existent backreference used the replacement text
}
Spidfire
  • 5,433
  • 6
  • 28
  • 36
  • As you can see im more a regexfreak than a java programmer (i guess those catches are not all needed) – Spidfire Dec 04 '10 at 21:37
0
public static String removeSpaces(String s) {
    StringTokenizer st = new StringTokenizer(s," ",false);
    String t = "";

    int i=0;  // counter variable

    while(st.hasMoreElements()){

       switch(i)
       {
          case 2:
          case 3:
                 t =  t + " ";
                 break;
          case 5:
                 t =  t + "  ";
                break;
          default:
                 t =  t + ";";
                 break;
       }

       t = t + st.nextElement();
       ++i;
    }

    t =  t + ";";  // put the last semi-colon

    return t;
}

Considering you want to put one blank character after the 2nd and 3rd words and two blank characters after the 5th word, you could use switch-case statements to achieve what you want.

user1234567
  • 603
  • 1
  • 4
  • 10