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
.– Guilherme Nascimento
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 ?
– Diego
@Guilhermenascimento is actually not wrong, the standard is even make the car return before, however
setText
andgetText
treat your content differently (just see in the implementation). Test by printing the values directly (System.out.println(texto.toString());
) and recovering from aJTextField
(System.out.println(MM.getText());
) and you’ll see the difference, recovering fromJTextField
will be a space before, this is due to how values are treated there :)– Bruno César
@Diego did not understand, you say the form using
charAt
? If it is it recovers one character at a time and treats it, already usingsplit
we have a vector ofString
, the final result is the same, the value oftexto
, includes both because it may be that you were trying something that you didn’t include in the question :)– Bruno César
@Brunocésar is then answered http://answall.com/a/7453/3635 would be partially incorrect?
– Guilherme Nascimento
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
@Diego http://meta.pt.stackoverflow.com/q/3918/3635
– Guilherme Nascimento
@Diego then that’s it right there, just continue your work. Any other questions just include new questions
– Bruno César
@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?
– Bruno César