static private String printTextPerRole(String[] roles, String[] textLines) { StringBuilder output = new StringBuilder(""); for (String role : roles) { output.append(role + ":\n"); for (int i = 0; i < textLines.length; i++) { if (textLines[i].startsWith(role & ": ")) { output.append(i + 1 + ") "); output.append(textLines[i].substring(role.length() + 2)); output.append("\n"); } } output.append("\n"); } return output.toString(); } 

How can I change the string to find the necessary characters if (textLines [i] .startsWith (role)) Since the string role (in the array are {Urbanity, Ammos Fedorovich}), there are lines without:, then I add ": \ n ". But since there are textLines lines in the second parameter — the same words are found, but already with double :: or a hyphen — the desired parameter cannot be identified. How to deal with this? Those. How to make the search more universal? (I hope I correctly expressed my thought)

  • where do you get these two arrays from? - Mikhail Vaysman
  • roles: Gorodnichy Ammos Fedorovich Artemy Filippovich Luka Lukich textLines: Gorodnichy: I invited you, gentlemen, in order to inform you of the unpleasant news: we are visited by an auditor. Ammos Fedorovich: How is the auditor? Artemy Filippovich: As an auditor? Mayor: Auditor from St. Petersburg, incognito. And with a secret prescription. Ammos Fedorovich: Those on! Artemy Filippovich: There was no concern, so give it up! Luka Lukic: Lord God! also with a secret prescription! - hellog888
  • These are 2 source arrays. Which need to be put in 1 line. - hellog888
  • The mayor: 1) I have invited you, gentlemen, in order to inform you of the most unpleasant news: an auditor is coming to us. 4) Examiner from St. Petersburg, incognito. And with a secret prescription. Ammos Fedorovich: 2) How is the auditor? 5) Here are the ones on! Artemy Filippovich: 3) How is the auditor? 6) There was no concern, so submit! Luka Lukic: 7) Lord God! also with a secret prescription! - hellog888
  • where do you get these arrays? - Mikhail Vaysman

1 answer 1

 private String printTextPerRole(String[] roles, String[] textLines) { //String resultString = "", temp1 = ""; StringBuilder resultString = new StringBuilder(); StringBuilder temp = new StringBuilder(); int count = 0; for (int i = 0; i < roles.length; i++) { resultString.append( roles[i]); resultString.append(":\n"); for (int j = 0; j < textLines.length; j++) { if (textLines[j].equals("0")) continue; else { //читаю автора while (textLines[j].charAt(count) != ':') { temp.append( textLines[j].charAt(count)); count++; } //если нужный автор if (roles[i].equals(temp.toString())) { count = count + 2; resultString.append(j + 1); resultString.append(") "); for (; count < textLines[j].length(); count++) { resultString.append( textLines[j].charAt(count)); } resultString.append("\n"); textLines[j] = "0"; } temp.setLength(0); count = 0; } } resultString.append("\n"); } return resultString.toString(); } 
  • I used another solution. - hellog888