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
110 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 64
type mismatch (Object[] to double[] 4 33
Notify sent to other threads in Java 9 34
Html Table Looping (part 2) 5 27
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

830 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