Android Integer.parseInt negative number - android

I'm new to development so sorry if my question is very simple.
I'm developing an app for D&D. When the user inserts a number in the first edittext i use onTextChanged of edittext so I setText to the second edittex the result.
My problem only occurs if the checkbox is checked. If the checkbox is not checked, it works fine but if the checkbox is checked the app will make a sum (proficiencybonus + mod). It works but only for positive numbers. When the app sets a velue of f.e. -5 the app crashes. The data is all saved in the sharedpreferences.
checkBox_strength.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox_strength.isChecked()) {
float result_num;
int num1, num2;
int f= 0;
DecimalFormat df = new DecimalFormat("0.##########");
num1 = Integer.parseInt(proficiencybonus.getText().toString());
if (strength_mod.getText().toString().length() > 0) {
num2 = Integer.parseInt(strength_mod.getText().toString());
} else {
num2=f;
}
result_num = num1 + num2;
strength_save.setText(" = " + df.format(result_num));
editor.putBoolean("checkBox_strength", true);
editor.apply();
}else{
editor.putBoolean("checkBox_strength", false);
editor.apply();
}
}
});
Have tried something else but I can't go find a solution
float result_num;
int num1, num2;
int f= 0;
DecimalFormat df = new DecimalFormat("0.##########");
num1 = Integer.parseInt(proficiencybonus.getText().toString());
if (strength_mod.getText().toString().length() > 0) {
num2 = Integer.parseInt(strength_mod.getText().toString());
} else {
num2=f;
}
result_num = num1 + Math.abs(num2);
strength_save.setText(" = " + df.format(result_num));
This is my error of the crash, it crashes "num2 = Integer.parseInt(strength_mod.getText().toString());"
2020-05-03 17:09:24.440 10318-10318/jekanapplication.charactersheet5e E/AndroidRuntime: FATAL EXCEPTION: main
Process: jekanapplication.charactersheet5e, PID: 10318
java.lang.NumberFormatException: For input string: "−1"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at jekanapplication.charactersheet5e.MainActivity$51.onCheckedChanged(MainActivity.java:1394)
at android.widget.CompoundButton.setChecked(CompoundButton.java:172)
at android.widget.CompoundButton.toggle(CompoundButton.java:128)
at android.widget.CompoundButton.performClick(CompoundButton.java:133)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

In the XML define this attribute: android:inputType="numberSigned"

The Problem is how set the text in the second edittext , is use onTextchange of the first edittext, I set the text edittext.settext"-2" I have changed in edittext.settext"R.String.two_" so when the i get the text I have no proble when I get the string.

Related

wrong calculation with double

I am working on a simple app, actually my first attempt in Android.
Expected result
I need the result to be shown in the English Language only, in case the mobile language is in Hindi for example.
I want the number to be shown without the letter (E) when the number is long.
Code
protected void operation(char ope){
double num1 = Double.parseDouble(txtNum1.getText().toString());
double num2 = Double.parseDouble(txtNum2.getText().toString());
double resultMulti = 0;
switch (ope) {
case '*':
resultMulti = num1 * num2;
long a3=(long)resultMulti;
if (resultMulti-a3 <= 0)
txtMultiResult.setText(a3 + "");
else
txtMultiResult.setText(String.format("%.2f",resultMulti));// without E but will be converted to Mobile Language
// txtMultiResult.setText(resultMulti+""); // with E
// txtMultiResult.setText(String.valueOf(resultMulti));// With E
// txtMultiResult.setText(Double.toString(resultMulti));// with E
imm.hideSoftInputFromWindow(btnMulti.getWindowToken(),0);
break;
}
}
How about using Locale.ENGLISH inside String.format()? As in like the following:
txtMultiResult.setText(String.format(Locale.ENGLISH, "%.2f", resultMulti));

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);
}

How can i get the tag of the checkboxes after they are checked?

I've tried this:
private int[] intCheckBox = {
R.id.monCheck,
R.id.tueCheck,
R.id.wedCheck
};
private CheckBox[] realCheckBox = new CheckBox[intCheckBox.length];
private int i;
Then:
for (i=0;i<intCheckBox.length;i++){
realCheckBox[i] = (CheckBox) findViewById(intCheckBox[i]);
realCheckBox[i].setTag(i);
realCheckBox[i].setOnCheckedChangeListener(showFinal);
}
}
private CompoundButton.OnCheckedChangeListener showFinal = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String outPut2 = "";
if (realCheckBox[i].isChecked()){
outPut2 += realCheckBox[i].getTag();
outPutText3.setText(outPut2);
}
However, when i ran the app, there was an error shown after i had checked one of the boxes, and the error was:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
I know there is another way to get the tag or even the string by setting them one by one... Yet, i would like to use the array to show the tag in order to do the further coding that to get the int value of "whatIReallyWantToGet":
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(Calendar.MONTH, position);
cal.set(Calendar.DAY_OF_WEEK, whatIReallyWantToGet);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
int month = cal.get(Calendar.MONTH);
String output = "";
while (cal.get(Calendar.MONTH) == month) {
output += cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1) + ",";
cal.add(Calendar.DAY_OF_MONTH, 7);
}
I am searching for a long time on net. But no use. I'm just a new learner from textbooks and online resources. Please help or try to give some ideas how to achieve this. Thank you so much!
From the code you shared, it seems to me that you are not resetting the value of the variable i.
That would explain why the exception says you are trying to access the index 3 of the array
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

Android - Program crashes when there is not input in editText fields

So i'm having a problem in my code which is probably simple to solve but its my first app so cut me some slack. When I enter no values in my editText box my app crashes. Im semi aware why it occurs but I cant seem to solve it.
public void onButtonClick( View v)
{
Double n1 , n2 , answer1;
EditText e1 = (EditText) findViewById(R.id.num1);
EditText e2 = (EditText) findViewById(R.id.num2);
TextView t1 = (TextView) findViewById(R.id.answer);
n1 = Double.parseDouble(e1.getText().toString());
n2 = Double.parseDouble(e2.getText().toString());
if (n1.toString().length()< 0 || n2.toString().length()< 0){
t1.setText("Please enter a number into both the base and height");
}else {
answer1 = ((n1 * n2) / 2);
t1.setText(Double.toString(answer1));
}
}
First check if there is input in both EditTexts and only if there is convert it to Double:
if (e1.getText().length() == 0 || e2.getText().length() == 0) {
t1.setText("Please enter a number into both the base and height");
} else {
n1 = Double.parseDouble(e1.getText().toString());
n2 = Double.parseDouble(e2.getText().toString());
answer1 = ((n1 * n2) / 2);
t1.setText(Double.toString(answer1));
}
EditText.getText() will never return null (as per source code) so it's safe to use methods on it (such as length().
if (!e1.getText().toString().isEmpty()) {
// do stuff
}

Dialog shows alpha Characters?

Am using Custom Dialog to shows the list of number based on user input,in Edittext box. I added Textwatcher everything is wrking fine until user try to give input more fast its showing one more Dialog with Black stripes with some alpha characters How to rectify this one?
My Screenshot is 1
Custom Dialog code here
this is my code which am using in textwatcher
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
str_length = str.length();
Log.v("length_before", "" + count + "" + str_length);
if (str_length == count + 1) {
return;
}
if (str_length >= 3) {
return;
}
if (str_length > count) {
count = str.length();
AmountDialog.amount_dialog(TicketIssueActivity.this, str,
amount);
} else if (str_length < count) {
count = str_length - 1;
Log.v("length slese", "" + count + "" + str_length);
}
}
If all you want is number in amount field. you can set:
android:inputType="number"
in the EditText field of your layout xml file.
That's all I got from the question. Please post more details or code if this doesn't solve the problem.

Categories

Resources