Separate each number with semicolon(;) in Java

Asked

Viewed 2,270 times

4

In a text box that has the variable name MMdados I put the numbers: 0;1;0

Then I created a variable String recebeMM who receives MMdados.getText();

I send these numbers to a text field that has the variable name MM causing these numbers to be below each other.

Ends up printing like this:

0
;
1
;
0

My problem is that the ; You have to separate the numbers and not show up and you can’t count the size of the recebeMM.length() in the for.

Code:

private void BenviarActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String recebeMM;

    recebeMM = MMdados.getText();

    String texto = "";

    recebeMM.split ("[;]");

    for(int i = 0; i < recebeMM.length(); i++){


        if(recebeMM != " "){ 


            texto = texto + recebeMM.charAt(i) + "\n";

        }

        MM.setText(texto);
    }

}

I used the recebeMM.split ("[;]");. But it didn’t work.

2 answers

7

split even receives a regular expression and yours is not wrong, since [] combines a single character of contained in [], for example to [abc] will match only a, b, or c. In your case it doesn’t matter ; or [;].

Assuming you need it, from the entrance 0;1;0, of a result like this:

0
1
0

Your problem is basically in the way you are recovering values after the split. This method returns a character array, so you need to assign this result to any variable, for example as follows:

final String[] values = recebeMM.split("[;]");

After this, in the for, instead of iterating the characters of recebeMM we will iterate the result vector of split. We can use a enhaced for , something like this:

for (final String value : values) {
    if (value.trim().length() > 0) {
        texto.append(value);
        texto.append("\n\r");
    }
}

Or the for pattern, something like that:

for (int i = 0; i < values.length; i ++) {
    final String value = values[i];
    if (value.trim().length() > 0) {
        texto.append(value);
        texto.append("\n\r");
    }
}

Note also this excerpt:

if(recebeMM != " ")

Well, this will always be true, since recebeMM is 0;1;0, then the only thing you’re doing is picking up character by character, including a \n between them and informing as text of MM.

A way to use the charAt how you used it would be more or less this, so we wouldn’t need the split:

final int length = recebeMM.length();
for (int i = 0; i < length; i++) {
    final char value = recebeMM.charAt(i);
    if (value != ' ' && value != ';') {
        texto.append(value);
        texto.append("\n\r");
    }
}

Another thing is that you must do the car return, using \r when assigning the value to a JTextField, for example, otherwise the result will be something like 0 1 0 by making a MM.getText(). For this, just use \n\r in place of only \n.

Finally a tip: always consider using StringBuilder (or even StringBuffer, depending on the context) rather than concatenate strings.

A version for your method BenviarActionPerformed would be this:

private void BenviarActionPerformed(final java.awt.event.ActionEvent evt) {
    final String recebeMM = MMdados.getText();

    final StringBuilder texto = new StringBuilder();
    final String[] values = recebeMM.split("[;]");

    for (final String value : values) {
        if (value.trim().length() > 0) {
            texto.append(value);
            texto.append("\n\r");
        }
    }

    MM.setText(texto.toString());
}
  • Bruno just a note, this is wrong the car should always come before the break, instead of \n\r use \r\n.

  • Thank you very much. That way it worked, but for me to continue with the work that is a cache simulator. This method I had never seen. Whether it says value gets each number or all together ?

  • @Guilhermenascimento is actually not wrong, the standard is even make the car return before, however setText and getText treat your content differently (just see in the implementation). Test by printing the values directly (System.out.println(texto.toString());) and recovering from a JTextField (System.out.println(MM.getText());) and you’ll see the difference, recovering from JTextField will be a space before, this is due to how values are treated there :)

  • @Diego did not understand, you say the form using charAt? If it is it recovers one character at a time and treats it, already using split we have a vector of String, the final result is the same, the value of texto, includes both because it may be that you were trying something that you didn’t include in the question :)

  • @Brunocésar is then answered http://answall.com/a/7453/3635 would be partially incorrect?

  • It was using Chaat but if in your case the result is the same has no problems. I have to take each position of a letter and convert it to binary and put in the validation bit, then I’ll take the number and calculate with it. In this case: int bitvalidate = 1; for (final String value : values){ if (value.Trim().length() > 0){ bin = toBinaryString (cont); int numero; numero = Integer.parseint(value); text.append(value +"-----" +bin +"------" +bitvalidation +"-----" +numero); text.append(" n");} MM.setText(text.toString()); }

  • @Diego http://meta.pt.stackoverflow.com/q/3918/3635

  • @Diego then that’s it right there, just continue your work. Any other questions just include new questions

  • @Guilhermenascimento is not wrong this response that linked, as said some components of swing treat different text, nor use these markers in swing. As the question here is not to be too extensive I put to generate the result that the AP expects. Anything look at the implementation of the methods I quoted or call me in the chat that I give you more details, OK?

Show 4 more comments

2

You have any array: list

Just do:

String myJoinedString = Joiner.on(";").join(list);

This code "joins" the elements of your list with the separator ;

EDIT: With this code, Joiner does not "leave" a separator at the end, and can be replaced by line break or anything else needed in place of the ;

Browser other questions tagged

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