Gift format text converter for Java language Moodle

Asked

Viewed 47 times

-1

I am beginner with programming mainly in Java, I have doubts about how to work with String in an arraylist, I would need to access each line in the text and modify it. Asterisk response * are correct

Input text the user types:
What’s between orange and green in the spectrum. Tick the alternative ?
*yellow
red
blue
green
purple


Output text after convert:
::Title:: What’s between orange and green in the spectrum. Check the alternative?{
*yellow
~red
~blue
~green
~purple
}

I don’t know how I can start the conversion in this arraylist

    List<String> giftFormat = new ArrayList<String>();
 
    giftFormat.add(txtEntradaDeTexto.getText());
    
    // Percorrer arraylist, realizando a formatação
    for (String integer: giftFormat) {
        
    }
  • Explain better what you want.

  • It would be a text conversion, take the string and put in an array or arraylist, at the end of the 1st line put key opening ( { ), inside the keys after finding the asterisk (correct answer) change by equal sign ( = ) and the other alternatives put til sign ( ~ ) that would be the correct answer, and below close the key ( } )

  • Each line is a value in the initial list?

  • That each line would be a value, first line the question, the other lines the alternatives and the last key line ( } )

1 answer

0


If I understood what you wanted try this.

List<String> giftFormat = //...

for (int i = 0; i < giftFormat.size(); i++) {
    String linha = giftFormat.get(i);
    if (i == 0)
        giftFormat.set(i, linha + "{");
    else if(linha.charAt(0) == '*')
        giftFormat.set(i, linha.replace("*", "="));
    else
        giftFormat.set(i, "~" + linha);
}
giftFormat.add("}");
  • It worked, I made an adjustment and I could convert, very sheltered helped a lot

Browser other questions tagged

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