using if statement with bigger than and less than - android

so what i have is 2 editText one for minimum number and one for maximum number and when the user clicks the button the app generate a random number between the 2 given number (I got it) but if the user entered the maximum number in the minimum number field the app crashes what i want to accomplish is if the user enters the maximum number in the minimum number field set the maximum number field to minimum number + 1 and try again automatically I tried the code below and its working if the user entered the values in the right please but if the user entered the maximum number in the minimum number field the app crash
Button gen = (Button)findViewById(R.id.button);
final EditText mini = (EditText)findViewById(R.id.mini);
final EditText maxi = (EditText)findViewById(R.id.maxi);
final TextView res = (TextView)findViewById(R.id.result);
final Random r = new Random();
final int[] number = {0};
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int minn = Integer.parseInt(mini.getText().toString());
int maxx = Integer.parseInt(maxi.getText().toString());
if (minn>=maxx){
maxi.setText(minn+1);
maxx = Integer.parseInt(maxi.getText().toString());
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}else{
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}
}
});
}

Replace the following...
maxi.setText(minn+1);
With...
maxi.setText(String.valueOf(minn+1));
Passing int value instead of String value into the setText method causing the crash problem.

I think you should do something like this:
if (minn>=maxx){
maxi.setText(String.valueOf(minn));
mini.setText(String.valueOf(maxx));
maxx = Integer.parseInt(maxi.getText().toString());
minn = Integer.parseInt(minn.getText().toString());
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));

Related

how to add 1 decimal place to score system

Im trying to make the score of my inspection have 1 decimal place so i can have more precise answer.
result = (TextView) findViewById(R.id.displaym1score);
sum=0;
total=0;
box1 = (CheckBox) findViewById(R.id.box1);
box1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (box1.isChecked()) {
sum += 1; int total = 100*sum/37;
result.setText(total + "%");
}
else {
sum -= 1; int total = 100*sum/37;
result.setText(total + "%");
}
}
});
each box you check it updates instantly and the score just needs to have a decimal place like "72.1%" instead of "72%". there is also 36 more boxes set up the same way.
Ive tried a few things and i cannot figure it out. what do i do?
First total must be a float or a double, not int
instead setText( ... put:
result.setText(String.format("%.1f", total) + "%"); // 1 is the decimal places you want
Probably the easiest way is to use a double instead of an int then format the output to display one decimal place.
use double for total and use following method to get your answer.
public double getFormattedTotal(double total){
DecimalFormat precision = new DecimalFormat("0.0");
return precision.format(total);
}

string to int,causing app to stop

final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
total_wt=ed11.getText().toString();
int rec = Integer.parseInt(recy_wt);
int nrec = Integer.parseInt(nonrec_wt);
ed11.setText(String.valueOf(rec + nrec));
The input I am giving is,ed7=10kg and ed8=20kg,and it will be displayed in ed11
as 30kg,but the app is unfortunately stopping.
If I am not writing 'kg',it is correctly displaying the totalwt.
But I want to display 'kg' in the answer.
There is already a solution here (can't flag as duplicate)
Find and extract a number from a string
Extract the digit part from the string and then parse it to an integer
Try this out
String regexp = "/^[0-9]+kg$/g"; //It accepts only '<number><kg>' format
int weight = 0;
if (recy_wt.matches(regexp) && nonrec_wt.matches(regex)) {
//It's valid input
Scanner scan = new Scanner(recy_wt);
weight = Integer.parseInt(scan.findInLine("\\d+(\\.\\d+)?"));
Scanner scan = new Scanner(nonrec_wt);
weight += Integer.parseInt(scan.findInLine("\\d+(\\.\\d+)?"));
}
ed11.setText(String.valueOf(weight + "Kg"));
If the only possible unit is kg, then try like this
final EditText ed7 = (EditText)findViewById(R.id.Recycleweight);
final EditText ed8 = (EditText)findViewById(R.id.Nonweight);
final EditText ed11 = (EditText)findViewById(R.id.totalweight);
int weight = 0;
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
recy_wt = recy_wt.replace("kg","");
nonrec_wt = nonrec_wt.replace("kg","");
weight += Integer.parseInt(recy_wt) + Integer.parseInt(nonrec_wt);
ed11.setText(weight + "kg");
This will work:-
recy_wt=ed7.getText().toString();
nonrec_wt=ed8.getText().toString();
String n=recy_wt.replace("kg","");
String n1=nonrec_wt.replace("kg","");
weight += Integer.parseInt(n) + Integer.parseInt(n1);
ed11.setText(weight + "kg");
storing the replaced one in a string and then converting to int will help.

I have 4 textviews which take their values from spinner. If TextView 3 and 4 are blank then I need to copy values from position 1 & 2

I have 4 textviews which take their values from dropdown list (spinner) selected at previous screen.
There can be either 2 or 4 numbers/letters as result of this selection.
The first position will always be a number and the second position will always be a letter. The third position can be a number or blank and the fourth position can be a letter or blank.
If position 3 and position 4 are blank then I need to make them equal to positions 1 & 2 respectively.
String myGrade = intent.getStringExtra("parameter_name_grade");
// above takes value of 'myGrade' from spinner selection at previous screen
String mDisplayGradeNumberEff = (" " + myGrade.charAt(0));
TextView displayGradeNumberEff = (TextView) findViewById(R.id.gradeNumberEffTV);
displayGradeNumberEff.setText(mDisplayGradeNumberEff);
String mDisplayGradeLetterEff = (" " + myGrade.charAt(1));
TextView displayGradeLetterEff = (TextView) findViewById(R.id.gradeLetterEffTV);
displayGradeLetterEff.setText(mDisplayGradeLetterEff);
// above works correctly
// from here down only works when a character is present in both positions
// if positions 3(2) and 4(3) are empty app stops running.
String mDisplayGradeNumberDia = (" " + myGrade.charAt(2));
if (mDisplayGradeNumberDia.isEmpty()) {
mDisplayGradeNumberDia = mDisplayGradeNumberEff;
}
TextView displayGradeNumberDia = (TextView) findViewById(R.id.gradeNumberDiaTV);
displayGradeNumberDia.setText(mDisplayGradeNumberDia);
String mDisplayGradeLetterDia = (" " + myGrade.charAt(3));
if (mDisplayGradeLetterDia.isEmpty()) {
mDisplayGradeLetterDia = mDisplayGradeLetterEff;
}
TextView displayGradeLetterDia = (TextView) findViewById(R.id.gradeLetterDiaTV);
displayGradeLetterDia.setText(mDisplayGradeLetterDia);
}
I Guess you have a array out of bounds exception, please provide Logcat....
Check if "myGrade" has 3/4 Characters, if it does not you can't read them with charAt(3)...
You can check the length of the String with "myGrade.length()"
When I asked this question I was fairly new to the site and didn't understand that I should post back the solution for future reference. Solution below worked so thanks to rocket for your help and sorry for the delay!
int myGradeLength = mGrade.length();
if (myGradeLength != 4) {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(0));
mDisplayGradeLetterDia = ("" + mGrade.charAt(1));
} else {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(2));
mDisplayGradeLetterDia = ("" + mGrade.charAt(3));
}

How to make invisible images visible?

I am developing a card game. I divide 13 cards to each client using Server,
when I divide 13 cards to 1st player, 9 cards are invisible and remaining 4 are visible.
Now I want when I click this one Image, the remaining 9 cards gets visible?
How to do this?
- the code is this:
String str=" "c,a", "c,k", "c,q", "c,j", "c,10", "c,9", "c,8", "c,7", "c,6", "c,5", "c,4", "c,3", "c,2"";
drawCards(str);
private void drawCards(String drawString) {
String[] separated = msgLog.split("\\,");
for (int i = 2; i < separated.length - 1; i += 2) {
String symbol = separated[i];
String num = separated[i + 1];
String resourceName = symbol + num;
//symbol and number is used for get image from xml file
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
im = (ImageView) findViewById(resID);
Context context = im.getContext();
cardID = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
//9 card invisible
if ( i > 10) {
im.setVisibility(View.GONE);
}
/* elseif(x.getVisibility() == VISIBLE)
{
x.setVisibility(INVISIBLE);
}*/
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 5, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(cardID);
im.setOnClickListener(this);
counter = counter + 8;
}
}
public void onClick(View v) {
final String IdAsString = v.getResources().getResourceName(v.getId());
pieceToast = Toast.makeText(getApplicationContext(), idServer, Toast.LENGTH_SHORT);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
pieceToast.show();
}
});
}
When I click this card how to hide card make visible
You can take the help from below given link:
https://stackoverflow.com/questions/41285814/how-to-make-visible-and-invisible-an-image-by-clicking-a-button-in-android-studi
If you still have question, let me know
You could play around with the visibility and use the methode setVisibility(View.VISIBLE) and setVisibility(View.GONE)

Android basic math in textview

I am trying to add two numbers and display them in the textview using this code. The problem here is that it doesn't add the numbers, it just displays the entire string.
CharSequence fnum, snum, symbol;
final TextView CalTextBox = (TextView) findViewById(R.id.MainTextview);
symbol = "+"; // addition selected
fnum = CalTextBox.getText(); // store number into fnum
snum = CalTextBox.getText(); //new number will be added in the code and be stored into snum
CalTextBox.setText(""); // delete whats in the text box
CalTextBox.setText(snum + "" + symbol + "" + fnum); // add two numbers
Well, the '+' operator performs concatenation if used on strings (like in this case). To perform a math operation, you have to convert them to numbers first. I think you can use this:
// Convert the 2 String to integer values
int first = Integer.valueOf(fnum);
int second = Integer.valueOf(snum);
// Compute the sum
int sum = first + second;
// Create the String you can use to display in the TextView
String textToDisplay = String.valueOf(sum);
snum = "2";
fnum = "3";
symbol = "+";
snum + "" + symbol + "" + fnum = "2+3"
Instead you should convert String into integer or double and make appropriate controls such as null or empty, or non-numeric then,
int result = Integer.parseInt(snum) + Integer.parseInt(fnum);
CalTextBox.setText("" + result);
For mathematical operations is best to use int, long or double variable types. Instead of CharSequence use for example int.
to get integer (int) from String (text) use:
int fnum, snum, symbol;
int fnum = Integer.parseInt("10"); or
fnum = Integer.parseInt(CalTextBox.getText());
CalTextBox.setText("" + (snum + symbol + fnum));

Categories

Resources