This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax Question IF ELSE (Java)
I am trying to make a calculator which shows a message if no value is entered in editbox. But it FC's!! I am making apps after long time so I am quite confused.
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
double a=0;
double b=0;
double c=0;
EditText edit;
EditText edit2;
TextView edit3;
String lname="";
edit=(EditText)findViewById(R.id.edit);
edit2=(EditText)findViewById(R.id.edit2);
edit3=(TextView)findViewById(R.id.edit3); // everything defined above
String editstr= edit.getText().toString(); // real work starts
if(editstr.contentEquals(lname))
edit3.setText("Enter value");
else
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
} };
put brackets around your if-else, currently in case of else it only executes the first line, other lines are executed no matter if your if passes or fails.
if(editstr.contentEquals(lname)) {
edit3.setText("Enter value");
} else {
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
}
As written, only the a=Double.parseDouble(edit.getText().toString()); is affected by the else. If you want the rest of it there, surround the block in {}
It does work, it just never fires.
You should do
if(editStr.isEmpty())
editStr = "Enter value";
else
{
//editStr.equals("someValue"); //test against some value
//rest
}
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
How to remove the last character from a string?
(37 answers)
What is a StringIndexOutOfBoundsException? How can I fix it?
(1 answer)
Closed 2 years ago.
I am making the calculator app.
I tried to make the delete button, but there are errors.
(1) If I press backspace when there is no number, the app closed suddenly.
(2) If I press a new number after deleting the number, the previous deleted number shows up again.
I searched a lot about it but I cannot understand them as a beginner.
I would appreciate it if you can explain it easily.
public class MainActivity extends AppCompatActivity {
TextView workingsTV;
TextView resultsTV;
String workings = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTextView();
}
private void initTextView()
{
workingsTV = (TextView)findViewById(R.id.workingsTextView);
resultsTV = (TextView)findViewById(R.id.resultTextView);
}
private void setWorkings(String givenValue)
{
workings = workings + givenValue;
workingsTV.setText(workings);
}
public void equalsOnClick(View view)
{
Double result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (Double) engine.eval(workings);
if (result != null)
{
int intVal = (int) result.doubleValue();
if (result == intVal)
{//Check if it's value is equal to its integer part
resultsTV.setText(String.valueOf(intVal));
}
else
{
resultsTV.setText(String.valueOf(result));
}
}
}
catch (ScriptException e) {
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
}
public void deleteOnClick(View view)
{
String del_number = workingsTV.getText().toString();
workingsTV.setText(del_number.substring(0,del_number.length() - 1));
}
Problem 1 is because del_number.length() is 0, so del_number.length()-1 is -1 which is an illegal parameter to substring. The easiest way to fix it is to not do anything if the length is 0.
Problem 2 is because you don't reset the variable workings when you delete. It needs to be set to "". Or don't have that variable at all and always use the workingsTV.getText() be the source of truth.
I found the solution.
I edit the code like this to prevent the app closed suddenly.
public void deleteOnClick(View view) {
if(workingsTV.getText().toString().length() >= 1) {
String getResultText = workingsTV.getText().toString();
String subString = getResultText.substring(0, getResultText.length() -1);
workingsTV.setText(subString);
}
else
{
workingsTV.setText(CLEAR_INT_TEXT);
}
}
But still I had a problem that the letter I deleted comes back when I press a new number.
The solution was this.
workings = workings.substring(0, workings.length() -1);
The letter at workings should be deleted as well like workingsTV
So here is full code for delete
public void deleteOnClick(View view) {
if(workingsTV.getText().toString().length() >= 1) {
String getResultText = workingsTV.getText().toString();
String subString = getResultText.substring(0, getResultText.length() -1);
workingsTV.setText(subString);
workings = workings.substring(0, workings.length() -1);
}
else
{
workingsTV.setText(CLEAR_INT_TEXT);
}
}
This question already has answers here:
Keep pressing a button so that a counter keeps adding by 1 every time
(2 answers)
Closed 5 years ago.
What i need this code to do is that I want it so that once you click the button for the first time, it displays if it's correct and if the button is pressed a second time it displays a new question.
I have updated the code. I have tried implementing the use of counters with the help of some comments however it still does not function correctly.
if(view.getId()==R.id.btnEnter){
counter++;
if(!answerDisplayed.endsWith("?")) {
int useranswer= Integer.parseInt(answerDisplayed.substring(5));
if(useranswer==calculatedAnswer){
//correct
}else{
//incorrect
}
}
if(counter ==2) {
randomQuestion();
}
}
counter = 0;
if(view.getId() == R.id.btnEnter && counter == 0) {
counter++;
// do something
}else{
randomQuestion();
}
at your randomQuestion(), you should decrease the counter.
void randomQuestion(){
.
.
.
--counter;
}
If the title is still a bit confusing what I really mean is that "when I click the button 1 followed by button 2 (with a bit time interval) it will generate a "letter" or "character" depending what values you coded on it it will input in textbox.
Please help I need this to complete my android application. I am having hard time dealing with this one.
My Code:
<Button
android:id="#+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textSize="16sp" />
inside my onCreate()
Button A = (Button) findViewById(R.id.block1);
Button B = (Button) findViewById(R.id.block2);
then setOnClickListener
A.setOnClickListener(this);
B.setOnClickListener(this);
inside my onClick() method and I did declare a global variable. String letter;
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.block1:
letter += "A";
tts.speak("A.", TextToSpeech.QUEUE_FLUSH, null);
break;
case R.id.block2:
letter += "B";
tts.speak("B.", TextToSpeech.QUEUE_FLUSH, null);
break;
.
.
just like that..
Please do tell me what I am missing or what is needed to revised. Please do correct me If I'm wrong.. That is already a working program, but as I said earlier that I want a **"one click after another" then it will generate a single "variable/character" in the textbox.
EDITED:
I will write a simple answer based on what I understand of what you are asking...
I suggest you add a variable,
boolean aPressed = false;
Then in the click listener for A change the variable to true, Start a timer and then have an if statement in your B button's click listener testing if A was clicked from the variable aPressed. Afterwards stop the timer, see if the time taken meets what you want and put the text inside the text box as you wish.
I will not go into greater detail as it is unclear of what you are asking and I am surprised this hasn't been closed.
Hope I helped,
-Daniel
The following is simple logic, not exact code that will work.
// Replace "String" with the correct type to suit the task
private String mButOneValue = "";
private void twoClickAction(String valueOne, String valueTwo) {
... perform the action here using the passed fields ...
}
// This would be your current switch statement maybe. This
// prevents a long method with possible code duplication.
private String getValueOfClick(View v) {
... extract the "value" you want to track ...
return theValueYouWorkedOut;
}
#Override
public void onClick(View v) {
if (mButOneValue.equals("")) {
// capture the first value you want to track.
mButOneValue = getValueOfClick(v);
} else {
// first click value is known, get the second and
// and perform the action you want.
twoClickAction(mButOneValue, getValueOfClick(v));
// And be sure to clear the value first value.
mButOneValue = "";
}
}
I am making a basic calculator for Android in Java. I am stuck in one final step where it must add the input with the previous input. Have a look at the code:
public void displayValue(){
String mything = display.getText().toString();
input = Integer.parseInt(mything);
}
public void number1(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(1));
displayValue();
}
public void number2(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(2));
displayValue();
}
public void plus(View view){
displayValue(); //result= 0
result = result + input; //result= input
input = 0; //input=0
//in this step input becomes 0 to let the user enter new number input but this
//input never add the old result and the equal shows the old result.
}
public void equal(View view){
displayValue();
display.setText(Integer.toString(result));
}
I noticed that if I add a line in equal method and add the result to input I get the correct answer but that's not gonna be helpful as there will be minus button too.
Any ideas?
Thanks.
It's definitely hard to tell because you don't include full code, which would be helpful, but could it be because you call displayValue(); in some places before doing the math? Specifically in the plus method.
Ok finally I came up with a solution.
As the only option for calculation of result and new input is in equal method (because logically when user press the equal button, so wants to ends the equation), so I added two boolean values for each minus and plus calculation. Then I added both calculation for adding or minus 2 values.
Then when the user inter first part of the calculation and then hit plus sign, the boolean value of plus becomes true and after entering new input and hitting the equal sign, it does the true part of the calculation. Well it is a bit hard to explain but i guess by looking at the code you would get what I mean.
boolean whenMinus = false;
boolean whenPlus = false;
public void plus(View view){
displayValue();
result = input;
input = 0;
whenPlus = true;
}
public void minus(View view){
displayValue();
result = input;
input = 0;
whenMinus = true;
}
public void equal(View view){
if (whenPlus == true){result = result + input; whenPlus = false;}
if (whenMinus == true){result = result - input; whenMinus = false;}
display.setText(Integer.toString(result));
}
I am not sure if it is the correct way of making this calculator. But I fixed my problem anyway. It would be great to comment me and let me know if it is the standard way or its kinda hacking. I am not a pro anyway.
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);
}