Application stopped working - java

I am making an android app in java.. And basicaly i have a problem.. I have a button which is removing the last char in a string.. And this button will be used alot and if the user is pressing the button while the string is empty the application gets an error message and closes.. And i want to prevent this somehow..
This is current code for the onClickListener..
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
value = value.substring(0, value.length() - 1);
numbersArea.setText(value);
}
Any idea how i would do this?? Since im out of ideas after trying all yesterday night.

Before substring, make sure the length of the string is > 0.
if (!value.isEmpty()) {
value = value.substring(0, value.length() - 1);
numbersArea.setText(value);
}
If you don't do this check, at some point the string will have zero length and substring() will throw IndexOutOfBoundsException.

Make your listener like this:
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!value.equals("") && value != null) {
value = value.substring(0, value.length() - 1);
numbersArea.setText(value);
}
}
Basically, what it does is verify if the string is empty and different of null, if its not, it enters in the if statement.
Please, let me know if you have more doubts.
Thanks.

Related

How to run Multiple case at a time in Android studio

I develop a program in android studio Where we check the number is odd or even. Everything works fine but I stuck in empty field case, I am unable to perform how to check the edit field is empty or not.
et_number = findViewById (R.id.et_number);
b_go = findViewById (R.id.b_go);
tv_show = findViewById (R.id.tv_show);
b_go.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick(View v) {
int number = Integer.parseInt (et_number.getText().toString ());
number = number %2;
String city = et_number.getText().toString();
if (number == 0|| city.isEmpty()){
tv_show.setText ("EVEN");
et_number.getText().clear();
Toast.makeText (MainActivity.this, "empty", Toast.LENGTH_SHORT).show ();
}
else {
tv_show.setText ("ODD");
et_number.getText().clear();
}
}
});
Add if condition at first line of on click listener like this
If( ! et_number.getTex().toString().equals(""))
And all your code inside this condition
.

Android, String Array can't go BACKWARDS from [0] TO [6(last String)] (Button = Button -1), CRASH

Whenever my back_button reaches the String[0] and I try proceeding going backwards my App just crashes.
Instead of simply going from String[0] to my currently last string [6] and continue to go backwards (if the conditions are met), why it doesnt do that ?
My code for that Button, btw im new to programming and I know my Code is EXTREMLY TRASH.. but, that's another topic, please xD :
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
forward_button.setVisibility(View.GONE);
backButton();
if (mediator == 10) {
forward_button.setVisibility(View.VISIBLE);
backk--;
display.setText(list[backk]);
}
if (backk == currentnumber-5 ) {
back_button.setClickable(false);
}
if (backk != currentnumber-5) {
back_button.setClickable(true);
back_button.setEnabled(true);
}
if (mediator != 10){
back_button.setEnabled(false);
display.setText(list[currentnumber]);
}
}
});
Btw. I thought it maybe has something to do with this Code right here in my other's Button Logic, maybe there is a similiar function to call for when going from String [0] to String [last string(6)] ?
if (currentnumber == list.length) {
currentnumber = 0;
backk = 0;
back = 1;
EDIT : I deleted if ( backk < 0 ) { ... , I dont know why it was in there to begin with, sorry, that wasnt supposed to be in there.
In your block, make the following change:
if (mediator == 10) {
forward_button.setVisibility(View.VISIBLE);
backk--;
// If we go below the size of the array, add the array
// size to loop back to the last element in the array
if (backk < 0){
backk += list.length;
}
display.setText(list[backk]);
}
If you explain what it is you're trying to accomplish and post more of your code, I might be able to help clean up your code a little with comments on why I make the choices I do. But the change above will fix that array out of bounds crash.

EditText field not appending text

I am fairly new to Android. I am making a very simply calculator.
For the Plus button I've written code to getText from the editText field store it in an array index for later addition and then show the + sign to be appended so the user can see the operation.
But for the code posted below, everything else executes except it doesn't show the + sign appended to the EditText view.
button_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(lower_textfield.length() == 0)
{
Toast.makeText(getApplicationContext(), "Write a number to add first",
Toast.LENGTH_SHORT).show();
}
else
{
tmp = lower_textfield.getText().toString();
arr[0] = Integer.parseInt(tmp);
lower_textfield.append("+");
}
}
});
Here tmp is String and arr is Int Array.
Help would be appreciated.
The method
append()
on the EditText object should must work.
Check the inputType of the EditText object.
May be mistakenly you've written any numeric inputType like.
android:inputType="numberDecimal"
It should be
android:inputType="none"
OR
android:inputType="text"
First of all, make sure you're running on the GUI thread. Never touch GUI from a non-GUI thread.
If that isn't the problem, try this instead:
tmp = lower_textfield.getText().toString();
arr[0] = Integer.parseInt(tmp);
lower_textfield.setText(arr[0] + "+");

App does not respond while trying to access charsequence elements through a loop

i m trying to make a calculator which can solve long expressions like 30+55-(2+7-20)
but i m having some logical problem in my equal button... beq
i have tested that logical problem is in for loop but couldnt understand. any help is greatl appreciated. heres my code
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (status==1){
get=tvdisp1.getText(); //tvdisp1 represents textView top expression bar
// tvdisp2.setText(get); //tvdisp2 represents 2nd textView answer bar
if(get.charAt(0)=='x') {status=0;} //to check for syntax error
if(get.charAt(0)=='รท') {status=0;} // same
for(int i =0; i <= get.length(); i++ ) { // loop to check if the first character is digit or character.
if (Character.isDigit(get.charAt(i))) {
//is digit
}
else {
//is operator
}
}
if (cbracq_c>obracs_c){status=0;} // if number of closing brackets > opening brackets
if ( status == 0 ) { tvdisp1.setText("Syntax Error AC to reset");}
}
}
});
it is just as Jon Skeet said.
You count 1 to far -> get.length() gives u a length (for example) 5, so you have index from 0 to 4. But in your loop you are also try to access index 5 because of the "<=". Just change it to "<" and it should work

Using While Loop after button press to search a multidimensional array for a value

I am relatively new to making applications for an Android phone and there is a problem that I have been trying to solve for the past week. What I am trying to do is take in 4 variables, 3 of which is from spinners and using while loops and case statements to search through the database to send a string into a TextView box. The main problem I have is the while loops after the button press errors out the program. I have tried implementing different ways like using a runnable and thread to work through it but have not had any success. I would greatly appreciate any help. The way the array is built is a 54x7.
public void onClick(View v) {
// TODO Auto-generated method stub
while(AutoDatabase[i][0] != YearSelect){
i++;
}
while(AutoDatabase[i][1] != MakeSelect){
i++;
}
while(AutoDatabase[i][2] != ModelSelect){
i++;
if (LightsOut == "FDTS"){
Part = AutoDatabase[i][3];
} else if (LightsOut == "FPTS"){
Part = AutoDatabase[i][4];
} else if (LightsOut == "RDTS"){
Part = AutoDatabase[i][5];
} else if (LightsOut == "RPTS"){
Part = AutoDatabase[i][6];
}
PartDisplay.setText(Part);
}

Categories