?
Solved

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

Posted on 2016-08-03
7
Medium Priority
?
149 Views
Last Modified: 2016-08-31
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
Comment
Question by:SunnyX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 2000 total points
ID: 41740347
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
 

Author Comment

by:SunnyX
ID: 41740883
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
 

Accepted Solution

by:
SunnyX earned 0 total points
ID: 41740923
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 41741076
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question