简体   繁体   中英

How to fetch a number from specific position in a string?

I have a string with more than 10 000 numbers separated by space:

String num = "1  34  16  9  9  9  12  44  1  ...";

I have a button where user clicks, the button gets an increment of +1 on every click. first click == 1, second == 2, third == 3, and so on.

this tells me how many clicks user has performed.

So what I want is this: on every click returns result from a string, I want to fetch a number from a string on a specific position/index. If it is first click, I will expect the first number in a string, if its second I expect second, third I expect a number on position 3 and so on...

For example if '75' is a position 25, assuming user is clicking the 25th click, the tv should show 75.

TextView tv = findViewById(R.id.result);
Button btn = findViewById(R.id.btn);

btn .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = 0;
            i++;
            String num = "1  34  16  9  9  9  12  44  1  ...";
            //search from string position i and get the number and show on tv
            //< some searching code here > to get result
            tv.setText("Result: " + result);
        }
    });

Where I have this "some searching code here", is where I want the function which does the logic to get the result from a string, when the result is gotten, I want to show on tv

You should split the numbers into an array. Since it's large, you should do it only once, so use a field in the anonymous class.

Since the code needs to remember and update the value of i , that variable also needs to be a field in the anonymous class.

btn.setOnClickListener(new View.OnClickListener() {
    private final String num = "1  34  16  9  9  9  12  44  1  ...";
    private final String[] numbers = num.trim().split("\\s+");
    private int i = -1;

    @Override
    public void onClick(View v) {
        if (++i == numbers.length)
            i = 0; // Roll over when reaching end of numbers
        tv.setText("Result: " + numbers[i]);
    }
});

First thing you have to Understand is that the previous value of i Will not be taken into account each time the user clicks because it's inside the Body of the onClick() it's always re-instatiated

To correct that and also split the Space separated String , you'll do:

TextView tv = findViewById(R.id.result);
Button btn = findViewById(R.id.btn);

btn .setOnClickListener(new View.OnClickListener() {
         //move the instantiation outside the OnClick
         int i = 0;
        @Override
        public void onClick(View v) {
           
            String num = "1  34  16  9  9  9  12  44  1  ...";

            String[] results = num.split("  ");
            tv.setText("Result: " + results[i]);
            i++;
        }
    });

I haven't tested this, but it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM