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?