user3441937
user3441937

Reputation:

Why when I remove toLowerCase() then not sort my words?

Why when I remove toLowerCase() then not sort my words? How I can fix this ? I won't lower case and large case in my xml? This is Java 8

My all code

class Word implements Comparable<Word>{
    private final String word;

    Word(String word) {
        this.word = word;
    }

    @Override
    public int compareTo(Word o) {
        return word.compareTo(o.word);
    }

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

class Sentence {
    private final List<Word> words;

    Sentence(List<Word> words) {
        Collections.sort(words);
        this.words = words;
    }

    public List<Word> getWords(){
        return words;
    }
}

interface Converter{
    String convert(Sentence sentence);
}



class CSVConverter implements Converter{

    @Override
    public String convert(Sentence sentence){
        return sentence.getWords().stream().map(Object::toString).collect(Collectors.joining(","))+"\n";
    }
}


//XML Converter
class XMLConverter implements Converter{

    @Override
    public String convert(Sentence sentence){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<sentence>\n");
        for(Word word : sentence.getWords()){
            stringBuilder.append("\t<word>");
            stringBuilder.append(word);
            stringBuilder.append("</word>\n");
        }
        stringBuilder.append("</sentence>\n");
        return stringBuilder.toString();
    }
}


//main class whit function to edit text writ whit Java 8
public class Nordea {
    public static void main(String[] args) throws IOException {
        Pattern unicodeWord = Pattern.compile("\\w+", Pattern.UNICODE_CHARACTER_CLASS);
        Converter converter = new CSVConverter();
        converter = new XMLConverter();

        Files.lines(Paths.get("text.txt"))
                .map(line -> line.toLowerCase().replaceAll("\\s+", " "))
                .flatMap(line -> Arrays.stream(line.split("[\\.!?]")))
                .map(sentence -> Arrays.stream(sentence.split(" "))
                                .filter(word -> unicodeWord.matcher(word).matches())
                                .map(Word::new)
                                .collect(Collectors.toList())
                )
                .filter(list -> list.size()>0)
                .map(Sentence::new)
                .map(converter::convert)
                .forEach(System.out::print);

        System.out.println();
    }
}

Why when I remove toLowerCase() then not sort my words? How I can fix this ? I won't lower case and large case in my xml? This is Java 8 This is Java 8

Upvotes: 0

Views: 93

Answers (2)

snaikar
snaikar

Reputation: 415

Try This it should work

@Override
public int compareTo(Word o) {
    String thisStr= this.word.toLower();
    String oStr=o.word.toLower();
    return thisStr.compareTo(oStr);
}

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

It would probably be best to implement a Comparator that lower-cases the text before comparing it.

This would, in my opinion, be better than doing the same in the Word object's compareTo method, unless you consider lower-case comparison to be the default for Word's 'natural' ordering.

Upvotes: 1

Related Questions