This program in each word of the text removes the same letters as the first.

Example:

[In]: Hehllo, what's up - Hah.

[Out]: Hello, what's up - Ha

But I ran into a problem in punctuation. I don’t know how to associate a ΠŸΡƒΠ½ΠΊΡ‚ΡƒΠ°Ρ†ΠΈΡ class with a Π‘Π»ΠΎΠ²ΠΎ / ΠŸΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ class, so that, let's say, the '-' sign is not a Π‘Π»ΠΎΠ²ΠΎΠΌ , since my word is separated by spaces on both sides. And that after each sentence put point \ question. mark - object ΠŸΡƒΠ½ΠΊΡ‚ΡƒΠ°Ρ†ΠΈΡ .

Code:

  public class Lab4 { public static void main(String[] args){ String s = "Π Π°Π·ΠΌΠ΅Ρ€ ΠΈΠ»ΠΈ Π΄Π»ΠΈΠ½Π° массива β€” это ΠΎΠ±Ρ‰Π΅Π΅ количСство элСмСнтов Π² массивС." + " Π Π°Π·ΠΌΠ΅Ρ€ массива задаётся ΠΏΡ€ΠΈ создании массива ΠΈ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΈΠ·ΠΌΠ΅Π½Ρ‘Π½ Π² дальнСйшСм," + " Π΄Ρ€ΡƒΠ³ΠΈΠΌΠΈ словами нСльзя ΡƒΠ±Ρ€Π°Ρ‚ΡŒ элСмСнты ΠΈΠ· массива ΠΈΠ»ΠΈ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΈΡ… Ρ‚ΡƒΠ΄Π°, Π½ΠΎ" + " ΠΌΠΎΠΆΠ½ΠΎ Π² ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΠ΅ элСмСнты ΠΏΡ€ΠΈΡΠ²ΠΎΠΈΡ‚ΡŒ Π½ΠΎΠ²Ρ‹Π΅ значСния."; Text text = new Text(s); System.out.println("Initial text:"); System.out.println(text.getAllText()); StringBuilder newText = new StringBuilder(); Sentence c; Word w; for(int i = 0; i < text.getSentences().length; i++){ c = new Sentence(text.getSentenceByIndex(i)); for(int j = 0; j < c.getWords().length; j++){ w = new Word(c.getWords()[j].getAllWord()); newText.append(w.removeChars()); newText.append(" "); } } System.out.println(newText); } } class Text { private Sentence[] sentences; private String allText; public Text(String allText){ String newAllText = allText.replaceAll("\\s+", " "); setAllText(newAllText); sentences = new Sentence[amountOfSentences()]; fillUpSentenceArray(); } public int amountOfSentences(){ return allText.split("\\.!?").length; } public void fillUpSentenceArray(){ for (int i = 0; i < amountOfSentences(); i++){ sentences[i] = new Sentence((allText.split("\\.!?"))[i]); } } public String getAllText() { return allText; } public Sentence[] getSentences() { return sentences; } public String getSentenceByIndex(int i){ return sentences[i].getAllSentence(); } public void setAllText(String allText) { this.allText = allText; } } class Sentence { private Word[] words; private Punctuation[] punctuations; private String allSentence; public Sentence(String allSentence){ this.allSentence = allSentence; words = new Word[countAmountOfWords()]; fillUpWordsArray(); punctuations = new Punctuation[countAmountOfPunct()]; fillUpPunctArray(); } public int countAmountOfWords(){ return allSentence.split(" ").length; } public int countAmountOfPunct(){ StringBuilder a = new StringBuilder(); for(int i = 0; i < allSentence.length(); i++){ if(allSentence.toCharArray()[i] == ',' || allSentence.toCharArray()[i] == '.' || allSentence.toCharArray()[i] == '-'){ a.append(allSentence.toCharArray()[i]); } } return a.length(); } public void fillUpWordsArray(){ for(int i = 0; i < countAmountOfWords(); i++){ words[i] = new Word((allSentence.split(" "))[i]); } } public void fillUpPunctArray(){ char[] charr = allSentence.toCharArray(); for(int i = 0; i < countAmountOfPunct(); i++){ if(charr[i] == ',' || charr[i] == '.' || charr[i] == '-'){ punctuations[i] = new Punctuation(charr[i]); } } } public Word[] getWords() { return words; } public String getAllSentence() { return allSentence; } } class Word { private Letter[] letters; private String allWord; public Word(String allWord){ this.allWord = allWord; letters = new Letter[countLengthofWord()]; fillUpLetterArray(); } public int countLengthofWord(){ return allWord.length(); } public void fillUpLetterArray(){ for (int i = 0; i < countLengthofWord(); i++){ letters[i] = new Letter(allWord.toCharArray()[i]); } } public StringBuilder removeChars(){ StringBuilder upDatedWord = new StringBuilder(countLengthofWord()); if (allWord.isEmpty()){ allWord = " "; } char distChar = allWord.charAt(0); upDatedWord.append(distChar); for (int i = 0; i < allWord.length(); i++){ if (allWord.isEmpty()){ continue; } if (Character.toLowerCase(distChar) != Character.toLowerCase(allWord.charAt(i))){ upDatedWord.append(allWord.charAt(i)); } } this.allWord = upDatedWord.toString(); return upDatedWord; } public String getAllWord() { return allWord; } } class Punctuation { private char symbol; public Punctuation(char symbol){ this.symbol = symbol; } public char getSymbol() { return symbol; } public void setSymbol(char symbol) { this.symbol = symbol; } } class Letter { private char letter; public Letter(char letter){ this.letter = letter; } public char getLetter() { return letter; } public void setLetter(char letter) { this.letter = letter; } } /*public void fillUpSentencePartArray(){ boolean flag = false; for(int i = 0; i < allSentence.toCharArray().length; i++){ if(Character.isLetter(allSentence.toCharArray()[i])){ char[] temp = new char[20]; temp[i] = allSentence.toCharArray()[i]; if(flag){ Word wordToAdd = new Word(temp.toString()); sentenceParts[i] = wordToAdd; } } else if(allSentence.toCharArray()[i] == '.' || allSentence.toCharArray()[i] == ','){ Punctuation znakToAdd = new Punctuation(allSentence.toCharArray()[i]); sentenceParts[i] = znakToAdd; } else flag = true; } } */ 
  • Why don't you create a SentencePart interface without methods. Let it encapsulate Word and Punctuation. Then in the Sentence class, you can use SentencePart [] instead of Word []. This will help preserve the order of words and punctuation in the sentence. - Alexander Potashev
  • Is the object Punctuation process then as Word? - Artem Aleksandrovich
  • What do you mean by the term handle? If you want to "process" only words, whatever is meant by this term, you can check the class of an object (instanceof) and if it is Word, then cast the object to it and "process" - Alexander Potashev
  • We need to make an array of words and punctuation in the interface? - Artem Aleksandrovich
  • Can you write how to start? I caught the essence, but I misunderstand how to implement ... - Artem Aleksandrovich

1 answer 1

I can write:

 interface SentensePart {} class Word implements SentensePart {...} class Punctuation implements SentensePart {...} class Sentence { //private Word[] words; //private Punctuation[] punctuations; //вмСсто этих Π΄Π²ΡƒΡ… строк с массивами классов ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ ΠΎΠ΄ΠΈΠ½ массив с ΠΎΠ±Ρ‰ΠΈΠΌ интСрфСйсом: private SentensePart[] sentenseParts; //ПослСдний элСмСнт Π² этом массивС Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‚ΡŒ классу Punctuation 

To understand that we have the word, you can use instaceof and casting to the class:

 SentensePart sentensePart = ...; if (sentensePart instanceof Word) { Word word = (Word) sentensePart; //ΠΈ Π΄Π°Π»Π΅Π΅ Ρ€Π°Π±ΠΎΡ‚Π°Π΅ΠΌ ΡƒΠΆΠ΅ с Ρ‚ΠΈΠΏΠΎΠΌ word 

I didn’t read your methods, but what else can I notice:

  1. why do you even need the Letter class? What is not satisfied with the Character?
  2. Punctuation contains only one character. And dots than offended?
  3. can be not only a punctuation mark, but also a hyphen.

UPD. To comment:

 public void fillUpSentencePartArray(){ boolean flag = false; for(int i = 0; i < allSentence.toCharArray().length; i++){ //ΠΌΠΎΠΆΠ½ΠΎ Π²ΠΎΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒΡΡ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ allSentence.charAt(i) if(Character.isLetter(allSentence.toCharArray()[i])){ //Π—Π΄Π΅ΡΡŒ Π²Ρ‹ создаСтС Π½ΠΎΠ²Ρ‹ΠΉ массив ΠΏΡ€ΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π½ΠΎΠ²ΠΎΠΉ Π±ΡƒΠΊΠ²Π΅, //старый ссылка массив ΠΏΡ€ΠΈ этом тСряСтся. ВмСсто массива //фиксированной Π΄Π»ΠΈΠ½Ρ‹ ΠΈΠΌΡ…ΠΎ Π²ΠΎΠΎΠ±Ρ‰Π΅ Π»ΡƒΡ‡ΡˆΠ΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ //StringBuilder. ΠžΠ±ΡŠΡΠ²Π»ΡΡ‚ΡŒ Π΅Π³ΠΎ ΠΈΠ»ΠΈ массив Π½Π°Π΄ΠΎ Π΄ΠΎ Ρ†ΠΈΠΊΠ»Π°, //Ρ‡Ρ‚ΠΎΠ±Ρ‹ ссылка Π±Ρ‹Π»Π° Π΄Π΅ΠΉΡΡ‚Π²ΠΈΡ‚Π΅Π»ΡŒΠ½Π° ΠΏΡ€ΠΈ Ρ€Π°Π·Π½Ρ‹Ρ… итСрациях Ρ†ΠΈΠΊΠ»Π° char[] temp = new char[20]; //Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΉ строкС Π²Ρ‹ ΠΎΠ±Ρ€Π°Ρ‰Π°Π΅Ρ‚Π΅ΡΡŒ ΠΊ элСмСнту массива temp, //с Π΄Π»ΠΈΠ½ΠΎΠΉ 20, ΠΏΠΎ индСксу i ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Π΄ΠΎ //allSentence.length temp[i] = allSentence.toCharArray()[i]; if(flag){ Word wordToAdd = new Word(temp.toString()); //здСсь ΠΎΠΏΡΡ‚ΡŒ ошибка с индСксом. i - это индСкс символа, //Π° Π½Π΅ индСкс Π² массивС sentenceParts. sentenceParts[i] = wordToAdd; } } else if(allSentence.toCharArray()[i] == '.' || allSentence.toCharArray()[i] == ','){ Punctuation znakToAdd = new Punctuation(allSentence.toCharArray()[i]); //Ρ‚Π° ΠΆΠ΅ ошибка с индСксом. sentenceParts[i] = znakToAdd; } else flag = true; } } 
  • 1.So the task says. 2. I didn’t add it because I first wanted to test for performance. - Artem Aleksandrovich
  • And how do we fill the array? So that everything in the right order could be filled up - Artem Aleksandrovich
  • You can parse the text by character - Alexander Potashev
  • In my code, the word can be with a punctuation mark, but I need them to be separately (for example, Ρ‚ΡƒΠ΄Π°, is this word) and how can we parse if we have an array of objects? - Artem Aleksandrovich
  • So what's the difficulty: read the character, if not a space and not a punctuation mark, then add to the current word. If the space - means the word is over, and add it to the array sentensePart. If the punctuation mark, then add the word first, then the punctuation mark. - Alexander Potashev