1
I’m trying to generate a random number in a user-given range. Android Studio does not point error, however the app does not generate any value. What may be wrong? Thanks in advance!
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView generate = findViewById(R.id.Generator);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText minValue = findViewById(R.id.Min);
EditText maxValue = findViewById(R.id.Max);
TextView finalValue = findViewById(R.id.Value);
String minString = String.valueOf(minValue);
String maxString = String.valueOf(maxValue);
int min = Integer.parseInt(minString);
int max = Integer.parseInt(maxString);
if (max > min) {
Random randGen = new Random();
int generate = randGen.nextInt((max - min) + 1) + min;
String genString = String.valueOf(generate);
finalValue.setText(genString);
}
}
});
}
}
If my answer was helpful, please accept it as a response to the left side and if possible mark it as useful.
– Murillo Comino