X
X
xaiponews2020-06-30 01:29:44
Java
xaiponews, 2020-06-30 01:29:44

Android Studio what's wrong with the code?

Android Studio what's wrong with the code? I'm trying to display the result of Randoma with a probability. The code is running. I press the button - the application crashes with an error. (This is a code mockup that will be pasted into the main application later).

package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

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

        Button cmak = (Button)findViewById(R.id.knopka);
        final TextView ocko = (TextView)findViewById(R.id.okno);

        cmak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ocko.setText(String.valueOf(foo()));
                ArrayList<Integer> gold = (ArrayList<Integer>) foo();
                Log.i("Gold", "" + gold.get(0) + "," +
                        gold.get(1) + "," +
                        gold.get(2) + "," +
                        gold.get(3) + "," +
                        gold.get(4));
            }

            private Serializable foo(){
                ArrayList<Integer> foo = new ArrayList<>();

                for (int i = 0; i< 5; i++) {
                    double random = Math.floor(Math.random() * 100) + 1;
                    if (random < 36) {
                        return 1;
                    } else if (random < 61) {
                        return 2;
                    } else if (random < 81) {
                        return 3;
                    } else if (random < 96) {
                        return 4;
                    } else if (random < 98) {
                        return 5;
                    }
                }
                return foo;
            }
        });
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2020-06-30
@xaiponews

The logic in the code is broken.
For some reason, the foo() method returns:
- either a number from 1 to 5, if at any step of the loop in random we got a number less than 98.
- or an empty list foo (if random turned out to be a number higher than 98 5 times).
Further, in the onClick() method, after calling foo(), the casting to ArrayList occurs.
This is where the application crashes (probably with a ClassCastException) if foo returned a number and not an ArrayList.
If foo() still returns an ArrayList, then the application will still crash when calling gold.get(0) with an IndexOotOfBoundException error, because the list is empty.
If I understand correctly what the author wanted to do, then all these return 1, return 2, etc. are needed in the foo () method. replace with adding a number to the list.
if (random < 36) {
foo.add(1);
} else if (random < 61) {
foo.add(2);
} ... etc.

X
xaiponews, 2020-06-30
@xaiponews

Maybe someone will need a solution. Android Studio Random with Probability. I myself searched for a long time, there was no intelligible sample.

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

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

        Button cmak = (Button)findViewById(R.id.knopka);
        final TextView ocko = (TextView)findViewById(R.id.okno);

        cmak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ocko.setText(String.valueOf(foo()));
            }

            private Serializable foo(){
                ArrayList<Integer> foo = new ArrayList<>();

                for (int i = 0; i< 5; i++) {
                    double random = Math.floor(Math.random() * 100) + 1;
                    if (random < 36) {
                        foo.add(1);
                    } else if (random < 61) {
                        foo.add(2);
                    } else if (random < 81) {
                        foo.add(3);
                    } else if (random < 96) {
                        foo.add(4);
                    } else if (random < 98) {
                        foo.add(5);
                    }
                }
                return foo;
            }
        });
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question