Robin Pe
Robin Pe

Reputation: 43

Reverse all words, set "." and do the same for the next sentences

I´m trying to program for so long now and it won´t work. I need a program, which does following:

User Input: I´m looking for some food. I love food. System Print: Food some for looking I´m. Food love I.

Reverse the words, UpperWord the first word, start with next sentence a keep the "." at it´s place.

package finaledition;

public class finaledition {
    public static void main(String[] args) {
        StringBuilder outputString= new StringBuilder();
        String satz;
        String wort;

        System.out.print("Bitte geben Sie einen String ein: ");
        String text = Input.readString();

        while (text.indexOf('.') > 0) {
            satz = text.substring(0, text.indexOf('.'));
            text = text.substring(text.indexOf('.') + 1);

            while (satz.lastIndexOf(' ') > 0) {
                wort = satz.substring(satz.lastIndexOf(' ') + 1);
                outputString.append(wort);
                satz = satz.substring(0, satz.lastIndexOf(' '));
            }
            System.out.print(outputString);
        }
    }
}

The actual result is:

foodsomeforlookingfoodsomeforlookingfoodlove

Upvotes: 0

Views: 70

Answers (2)

fxrbfg
fxrbfg

Reputation: 1786

Nested loops is hard to understand and leads to errors and time wasting in attempts to dubug and understand that procedural code, instead of this you need to use oop, java is oop language. You need to create class Sentence that completely resolve your problem.

import java.util.*;
import java.util.stream.*;

public class Main {

    public static void main(String[] args) {
        final String userInput = "I´m looking for some food. I love food";
        final String expectedResult = "Food some for looking I´m. Food love I.";
        String[] sentences = userInput.split("\\. ");
        String reversedSentences = Stream.of(sentences)
                .map(sentenceString -> new Sentence(sentenceString))
                .map(sentence -> sentence.reverse())
                .map(sentence -> sentence.firstLetterToUpperCase())
                .map(sentence -> sentence.removeAllDots())
                .map(sentence -> sentence.dotInTheEnd())
                .map(sentence -> sentence.toString())
                .collect(Collectors.joining(" "));
        System.out.println(reversedSentences.equals(expectedResult)); //returns true
    }


}

final class Sentence {
    private final String sentence;

    Sentence(String sentence) {
        this.sentence = sentence;
    }

    Sentence reverse() {
        String[] words = sentence.split(" ");
        Collections.reverse(Arrays.asList(words));
        return new Sentence(String.join(" ", words));
    }

    Sentence firstLetterToUpperCase() {
        String firstLetter = sentence.substring(0, 1);
        String anotherPart = sentence.substring(1);
        return new Sentence(firstLetter.toUpperCase() + anotherPart);
    }

    Sentence dotInTheEnd() {
        return new Sentence(sentence + ".");
    }

    Sentence removeAllDots() {
        return new Sentence(sentence.replaceAll("//.", ""));
    }

    @Override
    public String toString() {
        return sentence;
    }
}

Upvotes: 1

TVASO
TVASO

Reputation: 501

First, you need to split your hole text into sentences:

String[] sentences = text.split(". ");

Then you need to make an arrays with the words:

String[] words = sentences[anIndex].split(" ");

Then, reverse the words array:

String[] reverseSentence = new String[words.length];
for(int a=0; a<words.length; a++){
    reverseSentence[a] = words[words.length - a -1];
}

And then add all the reversed sentences together and add a '.'.

Upvotes: 1

Related Questions