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
76 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
  • 2
  • 2
7 Comments
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now