Reputation: 1339
The code below is trying to take two EditText values and then turn them to integers then divide them and output that number. I've had no luck and at every turn am just meeting roadblock after roadblock. I don't understand why it seems so difficult to just get two values from a user and divide them. If anyone has an answer please explain it in detail because I am new to Java/Android and want to understand why things are happening. I'm just frustrated now as I've been pounding my head at this one single problem for a week.
package com.simplesavinggoal;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
int finalGoal;
EditText goalInput;
EditText monthsNum;
Button enterGoal;
TextView goalOutput;
int goalInputInt;
int monthsNumInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterGoal = (Button) findViewById(R.id.btGetGoal);
goalOutput = (TextView) findViewById(R.id.tvSavingsGoalAmount);
goalInput = (EditText) findViewById(R.id.ndSavingsAmount);
monthsNum = (EditText) findViewById(R.id.ndMonthsNum);
enterGoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goalInputInt = Integer.parseInt(goalInput.getText().toString());
monthsNumInt = Integer.parseInt(monthsNum.getText().toString());
finalGoal = goalInputInt / monthsNumInt;
goalOutput.setText(finalGoal);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 0
Views: 811
Reputation: 1521
setText
for TextView
should be setText(CharSequence,TextView.BufferType)
.
Cannot input int
data type
Just Change to
goalOutput.setText(""+finalGoal);
and I think finalGoal should not be int
because the result of the /
will be decimal.
change the datatype to double
or something else
private double finalGoal;
Upvotes: 2
Reputation: 1
I think, set text in android textview must be a string. If you give an integer, then it search the String in the resources with the given integer as the id. So, do this instead:
goalOutput.setText(Integer.toString(finalGoal));
Upvotes: 0