[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Core Java. Splitting the string by words. How to take into account ". " ( dot ) at the end of the sentance.

import java.util.Arrays;

public class StringModificator {


	public static String firstLastWordsSwapper(String initialString) {

	    String[] words = initialString.split("\\s+"); // Split on any whitespace // 

	    if (words.length > 1) { 
	    	String lastword = words[words.length-1];
	        String firstword = words[0];
	        words[0] = lastword;
	        words[words.length-1] = firstword;
	        initialString = Arrays.toString(words).replaceAll("[\\[\\]\\,]", "");
	    }
	    
	    return initialString ;
	    
	}

}

Open in new window


How to take into account ". " ( dot ) at the end of the sentance ?
For example :
Initial input : "London is the capital of Great Britain."
Expected output : "Britain is the capital of Great London."
Actual output : "Britain. is the capital of Great London"   ("dot" moved with the last word - this is bug that I would like to solve )

Looking forward for reply from you dear Experts !
Best Regards,
Sunny
0
SunnyX
Asked:
SunnyX
  • 2
  • 2
2 Solutions
 
krakatoaCommented:
import java.util.Arrays;

public class StringModificator {


	//public static String firstLastWordsSwapper(String initialString) {
	public static void main(String[] args){
	
	    String initialString = "London is the capital of Great Britain.";
	    String[] words = initialString.split("\\s+"); // Split on any whitespace // 

	    if (words.length > 1) { 
		String finalPunctMark = words[words.length-1].substring(words[words.length-1].length()-1);
	    	String lastword = words[words.length-1].substring(0,(words[words.length-1].length()-1));
			
	        String firstword = words[0];
	        words[0] = lastword;
	        words[words.length-1] = firstword+finalPunctMark;
	        initialString = Arrays.toString(words).replaceAll("[\\[\\]\\,]", "");
	    }
	    
	    //return initialString ;
	    System.out.println(initialString);
	}

}

Open in new window


You could also add a bit more flexibility if you use smthg like :

finalPunctMark = Character.isLetterOrDigit(finalPunctMark.toCharArray()[0])?initialString.substring(initialString.length()-1):finalPunctMark;

Open in new window

to pick up any non letters and non digits in the final position.
0
 
SunnyXAuthor Commented:
Sorry that the first solution doesn't work is there is not "dot" at the end. The second solution "more flexible" is not compile if I put it in initialization finalPunctMark line ( line 13 ) or make no difference if I put it in the next line. Could you clarify when should I put this line of code
finalPunctMark = Character.isLetterOrDigit(finalPunctMark.toCharArray()[0])?initialString.substring(initialString.length()-1):finalPunctMark;

Open in new window

0
 
SunnyXAuthor Commented:
Based on your code, I made this solution. Thx

public static String firstLastWordsSwapper(String initialString) {
	   
	      
	   
	    String[] words = initialString.split("\\s+"); // Split on any whitespace // https://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters

	    

	    if (words.length > 1) { 
	    	
	    	// could be complication if there is punctuation mark in the end 
	    	// therefore logic is changing depending on punctuation in the sentence 

	    	if (Character.isLetterOrDigit(initialString.charAt(initialString.length()-1))){
	    		// in line above we will check is the last character of the sentence is letter or digit 
	    		String lastword = words[words.length-1];
		        String firstword = words[0];
		        words[0] = lastword;
		        words[words.length-1] = firstword;
		        initialString = Arrays.toString(words).replaceAll("[\\[\\]\\,]", "");
			
	    	}
	    	else {	// if the last character is not letter or digit than we will not transfer it at the beginning of the sentence 
	    		String finalPunctMark = words[words.length-1].substring(words[words.length-1].length()-1);
				
				finalPunctMark = Character.isLetterOrDigit(finalPunctMark.toCharArray()[0])?initialString.substring(initialString.length()-1):finalPunctMark;
			    	String lastword = words[words.length-1].substring(0,(words[words.length-1].length()-1));
					
			        String firstword = words[0];
			        words[0] = lastword;
			        words[words.length-1] = firstword+finalPunctMark;
			        initialString = Arrays.toString(words).replaceAll("[\\[\\]\\,]", "");
	    	}
		    }
	    
	    return initialString ;
	    
	}

Open in new window

0
 
krakatoaCommented:
I've been busy all day, but now I see your comment, I do not understand why you could not get my code to compile - it compiles fine for me. Perhaps post the errors if you are still interested in that, thanks.
1

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now