[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
?
158 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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

650 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