Return information from a String

Asked

Viewed 216 times

1

I’m trying to return the information from a String.

What I need is to pass a String parameter and return the information that is in the indicated places.

Ex.:

String FraseParametro = "<R1> casou com <R2>, e traiu ela com <R3>.";

String Frase = "João Pedro casou com Maria, e traiu ela com Joaquina Fofa.";

retorno = {{"<R1>","João Pedro"},{"<R2>", "Maria"}, {"<R3>", "Joaquina Fofa"}}

How can I do that?

My problem is that there are several different sentences and the position of <R > are different.

  • I don’t know if this can help you, but the <R1> is between the beginning of the string and the married word. The <R2> is between the comma and the <R3> is between the com and the endpoint. ;)

2 answers

2

A very simple way to do this is with replace.

String troca[][] = {{"<R1>","João Pedro"},{"<R2>", "Maria"}, {"<R3>", "Joaquina Fofa"}};
String FraseParametro = "<R1> casou com <R2>, e traiu ela com <R3>.";
System.out.println(FraseParametro);
for(int i = 0;i<troca.length;i++)
    FraseParametro = FraseParametro.replace(troca[i][0], troca[i][1]);
System.out.println(FraseParametro);

Try replacing what you have with a marker instead. So all that’s left is what you want.

String FraseParametro[] = {"casou com",", e traiu ela com"};
String Frase = "João Pedro casou com Maria, e traiu ela com Joaquina Fofa.";
for(int i = 0;i<FraseParametro.length;i++)
    Frase = Frase.replace(FraseParametro[i],";");

System.out.println(Frase);

String[] teste = Frase.split(";");
for(int i = 0;i<teste.length;i++)
    System.out.printf("\nIndice %s - Valor: %s",i,teste[i].trim());
  • Opa, but what I want is the opposite. A "Fraseparametro" I know that in this formed, but I do not know what is the value that is in the "<R >". Instead of exchanging I want to recover the value that is in place of the "<R >".

  • I supplemented the question, if it suits you so

  • 1

    Oops. Thanks, I think you can help me out!!

  • Blz, just don’t forget to put it away.

0

Possible solution:

public class App {

private List<String> keys;
private String fraseParametro;
private String frase;
private Map<String, String> map;

public App() {
    keys = new ArrayList<>();
    map = new HashMap<>();
}

public static void main( String[] args ) throws IOException, InterruptedException {
    String fraseParametro = "<R1> casou com <R2>, e traiu ela com <R3>.";

    String frase = "João Pedro casou com Maria, e traiu ela com Joaquina Fofa.";

    App app = new App();
    app.addKey("<R3>");
    app.addKey("<R1>");
    app.addKey("<R2>");

    app.setFrase(frase);
    app.setFraseParametro(fraseParametro);
    app.createPairKeyValue();
    Map<String, String> keyValue = app.getMap();
    System.out.printf("Frase Parâmetro: %s\n", app.getFraseParametro());
    System.out.printf("Frase: %s\n", app.getFrase());
    for (String key : keyValue.keySet()) {
        System.out.printf("%s: %s\n", key, keyValue.get(key));
    }

}

public void createPairKeyValue() {
    Collections.sort(keys);

    String findingSubstitues = frase;
    for (String key : keys) {
        String replacement = findValue(findingSubstitues, key);
        if (replacement != null) {
            map.put(key, replacement);
            findingSubstitues = findingSubstitues.replaceAll(replacement, key);
        }
    }
}

public Map<String, String> getMap() {
    return this.map;
}

private String findValue(String phrase, String key) {
    List<String> phraseWithKeys = getWords(fraseParametro);
    List<String> phraseWithoutKeys = getWords(phrase);
    int index = phraseWithKeys.indexOf(key);

    if (index == -1) {
        return null;
    }

    String replacement = phraseWithoutKeys.get(index);

    for (int i = index+1; i < phraseWithoutKeys.size(); ++i) {
        if (((index+1) == phraseWithKeys.size()) || !phraseWithoutKeys.get(i).equals(phraseWithKeys.get(index+1))) {
            replacement += " " + phraseWithoutKeys.get(i);
        } else {
            break;
        }
    }
    return replacement;
}

public void addKey(String key) {
    keys.add(key);
}

public void setFraseParametro(String fraseParametro) {
    this.fraseParametro = fraseParametro;
}

public void setFrase(String frase) {
    this.frase = frase;
}

public List<String> getWords(String phrase) {
    String[] words = phrase.split(" |,|\\.");
    return Arrays.asList(words);
}

public String getFraseParametro() {
    return fraseParametro;
}

public String getFrase() {
    return frase;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.