kubakyc
kubakyc

Reputation: 17

I am stuck on trying to return a certain part of a string because its a variable

Basically, for my assigned program I have to" "create a string by retrieving the last word from each sentence, removing any punctuation, except for the last word, and output this new string"

I am stuck on retrieving the last words as there are multiple variables inside of the string which changes the index of the whole string depending on user input.

I've tried playing around with the index and using indexOf but to be honest, I've hit a wall and I am completely stuck

String Sen1 = " There once was a person named " + firstName + " who lived in a city called " + city + ". ";

System.out.println("new phrase is: " + Sen1.substring(62, Sen1.length() - 2));

the code will work if I strictly use my personal information. but if another user inputs their info the string's index will change and the print will come out wrong. Am I using the right method for this type of problem? Is there an easier way to accomplish this?

Upvotes: 0

Views: 302

Answers (2)

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

What you want to do is utilize the String#split() method along with a Regular Expression to break up your paragraph into sentences. You can do it something like this:

String[] sentences = paragraph.split("\\.|\\!|\\?");

But before you do this, snag the last punctuation character used in the paragraph. Evidently, you're going to need this.

Now you need to iterate through all the Sentences within the sentences array and then split each sentence into individual words. Take the last word from each sentence and concatenate them all into a String variable (each separated with a whitespace or whatever). Now it's just a matter of displaying your concatenated string. It might go something like this:

String firstName = "Tom";
String city = "Los Angeles";
// A Paragraph (with indent)....
String paragraph = "   In a world far far away there once was a person named " + firstName + 
                   " who lived in a city called " + city + ". He was a tall " +
                   "man with dark hair. He liked to drink the comb water in " + 
                   "hair salons!";
// Trim off leading and trailing whitespaces, tabs, etc from your paragraph
paragraph = paragraph.trim();
// Collect the punctuation character used at the end of the paragraph.
// Since this will be your last sentence anyway you will need it.
String paragraphEndPunctuation = paragraph.substring(paragraph.length() - 1, paragraph.length());
// Split the Paragraph into Sentences based on the major characters for sentence end.
String[] sentences = paragraph.split("\\.|\\!|\\?");
// Create a StringBuilder object so as to creat the string 
// of detected last words in sentences.
StringBuilder sb = new StringBuilder();
// Iterate through each sentences and aquires the last word from each/
for (int i = 0; i < sentences.length; i++) {
    // Split the current sentence into individual words
    String[] words = sentences[i].trim().split("\\s+");
    // Add the last word of current sentence to the StringBuilder object.
    // Ternary Operator is used here to apply a whitespace after each word
    // if the StringBuilder object already contains something.
    sb.append(sb.toString().equals("") ? words[words.length-1] : " " + words[words.length-1]);
}
// Append the final punctuation character.
sb.append(paragraphEndPunctuation);
// Display in console.
System.out.println(sb.toString());

Upvotes: 0

Snagtz
Snagtz

Reputation: 118

Split your input string on spaces and return the last String from the resulting String array. Example:

 public static void main(String []args){
    String variable = "randomlongwords";
    String sentence = "Hello this is a " + variable + " random sentence. ";
    String[] words = sentence.split(" ");
    System.out.println(words[words.length - 1]);
 }

Output: sentence.

Upvotes: 1

Related Questions