C
C
Chvalov2014-12-17 16:56:15
Java
Chvalov, 2014-12-17 16:56:15

How to decompose a string with numbers into bytes and back in java (Android)?

Hello, there is a text field in which I enter numbers up to 8 characters long.
There are 8 text fields (Plain text) in which the number must be written byte by byte.
For example, I enter the number 57126973, then at the output in the text fields I should see
5
7
1
2
6
9
7
3
And then collect them back.
Tell me how to do it or give a link to an example.
thanks in advance
Here is a piece of code using Pavel as an example , but as I understand it, it is for pure java, but I need it for Android, help me finish it.

public class MainActivity extends ActionBarActivity {

    EditText enter_number_fields;     // Сюда я ввожу строку например 57126973
    EditText Text1;     // Текстовые поля в которых должен быть вывод по одному символу
    EditText Text2;     //....
    EditText Text3;
    EditText Text4;
    EditText Text5;
    EditText Text6;
    EditText Text7;
    EditText Text8;
    EditText Construct_text;     // Строчка которая собирет все обратно с 8 полей в одну строку

    Button btn_num_byte;     // Кнопка которая разбивает строку на 8 полей
    Button btn_num_text;     // Собирает обратно с 8 в одну

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // находим элементы
        enter_number_fields = (EditText) findViewById(R.id.enter_number_fields);
        Text1 = (EditText) findViewById(R.id.Text1);
        Text2 = (EditText) findViewById(R.id.Text2);
        Text3 = (EditText) findViewById(R.id.Text3);
        Text4 = (EditText) findViewById(R.id.Text4);
        Text5 = (EditText) findViewById(R.id.Text5);
        Text6 = (EditText) findViewById(R.id.Text6);
        Text7 = (EditText) findViewById(R.id.Text7);
        Text8 = (EditText) findViewById(R.id.Text8);
        Construct_text = (EditText) findViewById(R.id.Construct_text);

        btn_num_byte = (Button) findViewById(R.id.btn_num_byte);
        btn_num_text = (Button) findViewById(R.id.btn_num_text);
    }

/* Разбивка строки */
    public void btn_num_byte (View view)
    {
        Text1 = str[0];
        Text2 = str[1];
        Text3 = str[2];
        Text4 = str[3];
        Text5 = str[4];
        Text6 = str[5];
        Text7 = str[6];
        Text8 = str[7];
    }
/* Разбивка строки */

/* Собираем обратно */
    public void btn_num_text (View view)
    {
        Construct_text.setText(Text1 + " " + Text2 + " " + Text3 + " " + Text4 + " " + Text5 + " " + Text6 + " " + Text7 + " " + Text8);
    }
/* Собираем обратно */
Does not collect strings, type must be integer, after strings
Button btn_num_byte;
    Button btn_num_text;
I wrote String srt = enter_number_fields;an error occurs
if so
String srt = enter_number_fields.getText().toString();
then the error in this section swears atpublic void btn_num_byte (View view)str

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2014-12-17
@ProgramCodePav

Sorry for this code :)

string str = tbInputField.Text; // 57126973

tbPlaintxt1.Text = str[0];  //5
tbPlaintxt2.Text = str[1];  //7
tbPlaintxt3.Text = str[2];  //1 ...
tbPlaintxt4.Text = str[3];
tbPlaintxt5.Text = str[4];
tbPlaintxt6.Text = str[5];
tbPlaintxt7.Text = str[6];
tbPlaintxt8.Text = str[7];

Try something like this
Back:
In general, these are the basics. I advise you to read a Java book about string formatting and working with the String type.

N
Nikolai Pavlov, 2014-12-18
@gurinderu

Arrays.asList("123".split("")).stream().map(String::valueOf).mapToInt(Integer::valueOf).forEach(value -> add(new TextField(value)));
For java, you could write like this, for Android it won’t work)
In your case, I would use the example above to avoid overhead for all sorts of unnecessary things.

D
Dekker8, 2014-12-21
@Dekker8

You can try by adding elements to the activity...(layout.addView() <--- something like that....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question