How to set textview as method output in andrioid? - java

I have just started to use android studio recently and currently I am working on a Roman Numeral Translator app. The app's interface looks like this: Application interface
The user will use the keypad to input a integer which will show up on the TextView shown above. When they hit the convert button it will take the integer they have entered and convert it (the program will be able to catch the input if contains a string or character). Then the app will reset the TextView to the result after the user hits the "convert" button.
Currently, my main activity contains the onClickListeners for the buttons and a separate translator method for the translations. My problem is with the "convert" button I'm not sure how to get the input from the translator method and set it as TextView when it finishes converting. Here is the sample of my code:
"Convert" button listener- `
convert.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
TextView numeralInput = (TextView) findViewById(R.id.textView);
String intValue = numeralInput.getText().toString();
try{
int integer = Integer.parseInt(intValue);
if (integer > 0 && integer <= 4999){
translator(integer);
}else{
numeralInput.setText("Please enter an integer between 0 and 4,999.");
}
}catch(NumberFormatException e){
numeralInput.setText("Invalid input try again.");
}
}
}
);
`
Translator Method- `
public static void translator(int integer) {
LinkedList<String> stack = new LinkedList<String>();
// if (integer > 0 && integer <= 4999) {
//ArrayList<Integer> placement = new ArrayList<Integer>();
int place = (int) Math.log10(integer);
for (int i = 0; i <= place; i++) {
//while(integer > 0){
//System.out.println(integer);
int placeOfValue = integer % 10;
//stack.push(placeOfValue);
//System.out.print(stack);
//System.out.print(placeOfValue +":" + i);
String placement = "";
switch (i) {
case 0:
placement = ones(placeOfValue);
break;
case 1:
placement = tens(placeOfValue);
break;
case 2:
placement = hundreds(placeOfValue);
break;
case 3:
placement = thousands(placeOfValue);
break;
default:
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
stack.push(placement);
}
integer = integer / 10;
//System.out.print(placement);
// System.out.println(placement.size());
//}
// for(int j = 0; j < placement.size(); j++){
// double tenthPower = Math.floor(Math.log10(placement.get(j)));
// double place = Math.pow(10, tenthPower);
// System.out.println(place);
//
// }
// }
while (!stack.isEmpty()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
System.out.print(stack.pop());
}
}
// } else {
// System.out.println("Please enter an integer between 0 and 4,999.");
// }
}
}
`
The other methods inside translator is like a library for the roman numerals each containing a numeral for each of the place values as shown below.
Thousands method- `
public static String thousands(int integer) {
String thouValue = "";
switch (integer) {
case 1:
thouValue = "M";
//System.out.print("M");
break;
case 2:
thouValue = "MM";
//System.out.print("MM");
break;
case 3:
thouValue = "MMM";
//System.out.print("MMM");
break;
case 4:
thouValue = "MMMM";
//System.out.print("MMMM");
break;
default:
thouValue = "";
break;
}
return thouValue;
}
`

Make your translator() method return a string which contains the final output.
So before the while statement in that method, declare a string such as
String result = null; and in the loop append the popped values to this variable like, result += stack.pop().
Now, the place where you call the translator(integer) method, do numeralInput.setText(translator(integer)) instead of translator(integer)

You need to make your TextView a class member and initialise it in your oncreate bundle, so that you can access the textview elsewhere in the activity.
TextView numeralInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
numeralInput = (TextView) findViewById(R.id.textView);
convert.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
String intValue = numeralInput.getText().toString();
try{
int integer = Integer.parseInt(intValue);
if (integer > 0 && integer <= 4999){
translator(integer);
}else{
numeralInput.setText("Please enter an integer between 0 and 4,999.");
}
}catch(NumberFormatException e){
numeralInput.setText("Invalid input try again.");
}
}
}
);

Related

The textbox of the current Activity is not updated when another item is selected in the RadioButton of the previous Activity. Android/Java

The application should count the moments of inertia and moments of resistance of the profiles, and output the value of the selected profile in the TextView.
Application work:
The user enters the value of loads, deflection, length
Selects the required profile via radoiButton
Clicks on the "Execute calculation" button
The application reads and displays the value of the selected profile in the TextView
At the first launch, the application works correctly and everything is displayed. But when you exit to the previous activity to change the profile selection via radoiButton, the value of the text field does not change and remains the same as in the previous selection. Only restarting the entire application helps.
It turns out that to change the cross-section, you need to completely terminate the application each time and re-enter the data.
data input activity1
public void shvellerOnClick(View view) {
radioGroupShveller.removeAllViews();
RadioButton shvellerYRadioButton = new RadioButton(this);
RadioButton shvellerPRadioButton = new RadioButton(this);
shvellerYRadioButton.setText(R.string.shveller_Y);
shvellerPRadioButton.setText(R.string.shveller_P);
radioGroupShveller.addView(shvellerYRadioButton);
radioGroupShveller.addView(shvellerPRadioButton);
shvellerPRadioButton.setOnClickListener(radioButtonClickListener);
shvellerYRadioButton.setOnClickListener(radioButtonClickListener);
shvellerYRadioButton.setId(R.id.idShvellerYRadioButton);
shvellerPRadioButton.setId(R.id.idShvellerPRadioButton);
radioGroupShvellerX2.removeAllViews();
radioGroupDvutavr.removeAllViews();
radioGroupShvellerX2.clearCheck();
radioGroupDvutavr.clearCheck();
}
public void dvaShvelleraOnClick(View view) {
radioGroupShvellerX2.removeAllViews();
RadioButton shvellerYX2RadioButton = new RadioButton(this);
RadioButton shvellerPX2RadioButton = new RadioButton(this);
shvellerYX2RadioButton.setText(R.string.shveller_Y_x2);
shvellerPX2RadioButton.setText(R.string.shveller_P_x2);
radioGroupShvellerX2.addView(shvellerYX2RadioButton);
radioGroupShvellerX2.addView(shvellerPX2RadioButton);
shvellerYX2RadioButton.setOnClickListener(radioButtonClickListener);
shvellerPX2RadioButton.setOnClickListener(radioButtonClickListener);
shvellerYX2RadioButton.setId(R.id.idShvellerYX2RadioButton);
shvellerPX2RadioButton.setId(R.id.idShvellerPX2RadioButton);
radioGroupShveller.removeAllViews();
radioGroupDvutavr.removeAllViews();
radioGroupShvellerX2.clearCheck();
radioGroupDvutavr.clearCheck();
}
View.OnClickListener radioButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton rb = (RadioButton)v;
switch (rb.getId()) {
case R.id.idShvellerYRadioButton:
valueSelectedGost = 0;
break;
case R.id.idShvellerPRadioButton:
valueSelectedGost = 1;
break;
case R.id.idShvellerYX2RadioButton:
valueSelectedGost = 10;
break;
case R.id.idShvellerPX2RadioButton:
valueSelectedGost = 11;
break;
case R.id.idDvutavBRadioButton:
valueSelectedGost = 20;
break;
case R.id.idDvutavKRadioButton:
valueSelectedGost = 21;
break;
}
}
};
public void vypolnitRaschetOnClick(View view) {
int putGost = valueSelectedGost;
Intent performCalculationIntent = new Intent(StBalkaSchema1CopyActivity.this, StBalkaShema1RaschetCopyActivity.class);
performCalculationIntent.putExtra("gostInt", putGost);
startActivity(performCalculationIntent);
}
Activity2 calculation
Intent intent = getIntent();
int getGostInt = intent.getIntExtra("gostInt",0);
selectedProfileImageView = (ImageView) findViewById(R.id.selectedProfileImegeView);
selectedProfileTextView = (TextView) findViewById(R.id.selectedProfileTextView);
infoSelectedProfileTextView = (TextView) findViewById(R.id.infoSelectionProfileTextView);
double loadToFormula = Double.parseDouble(loadTextView.getText().toString());
double lengthToFormula = Double.parseDouble(lengthTextView.getText().toString());
double deflectionToFormula = Double.parseDouble(deflectionTextView.getText().toString());
double resultWtr = resultMmax * 100 / (1.12*2.1);
double resultItr = resultMmax * Math.pow(10, 5) * lengthMToFormula * Math.pow(10, 2) * deflectionToFormula / (10 * 2.1 * Math.pow(10, 6));
if (getGostInt >= 0 & getGostInt <= 9){
selectedProfileImageView.setImageResource(R.drawable.shveller);
if (getGostInt == 0){
ShvellerU_GOST_8240_89.addShvellerU();
infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y);
} else if (getGostInt == 1){
ShvellerP_GOST_8240_89.addShvellerP();
infoSelectedProfileTextView.setText(ShvellerP_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_P);
}else {
infoSelectedProfileTextView.setText("Профиль не выбран");
selectedProfileTextView.setText("Профиль не выбран");
}
} else if (getGostInt >= 10 & getGostInt <= 19){
selectedProfileImageView.setImageResource(R.drawable.dva_shvellera);
if(getGostInt == 10){
ShvellerUx2_GOST_8240_89.addShvellerUx2();
infoSelectedProfileTextView.setText(ShvellerUx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y_x2);
} else if (getGostInt == 11){
ShvellerPx2_GOST_8240_89.addShvellerPx2();
infoSelectedProfileTextView.setText(ShvellerPx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_P_x2);
} else {
infoSelectedProfileTextView.setText("Профиль не выбран");
selectedProfileTextView.setText("Профиль не выбран");
}
}
}
Profile adding method
public class ShvellerU_GOST_8240_89 extends Shveller {
public ShvellerU_GOST_8240_89(String name, double momentSoprotivleniya, double momentInertsii, double massa) {
super(name, momentSoprotivleniya, momentInertsii, massa);
}
public static void addShvellerU() {
ShvellerU_GOST_8240_89 shveller5U = new ShvellerU_GOST_8240_89("5У", 9.1,22.8,4.84);
shveller5U.putIn(shveller5U);
ShvellerU_GOST_8240_89 shveller6_5U = new ShvellerU_GOST_8240_89("6,5У", 15,48.6,5.9);
shveller6_5U.putIn(shveller6_5U);
ShvellerU_GOST_8240_89 shveller8U = new ShvellerU_GOST_8240_89("8У", 22.4,89.4,7.05);
shveller8U.putIn(shveller8U);
ShvellerU_GOST_8240_89 shveller10U = new ShvellerU_GOST_8240_89("10У", 34.8,174.0,8.59);
shveller10U.putIn(shveller10U);
}
}
The error will most likely occur in this block activity2 calculation.
if (getGostInt == 0){
ShvellerU_GOST_8240_89.addShvellerU();
infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y);
I think that this is due to the fact that when objects are added through the addShvellerU () method, they remain in memory and when a different section is selected, the addDvutavrK () method cannot overwrite another. How to solve this problem?

How to change ImageView of dice roller based on random result

I am building a Fate Dice roller app for my finishing course in App Development and I am having a problem with changing the ImageView of the dice based on the result. Here is the code(the roller is in a fragment tab):
public class RollDiceFragment extends Fragment {
private Button btnRoll;
private EditText et_rollDice;
private ImageView iv_dice1, iv_dice2, iv_dice3, iv_dice4;
Random random;
public RollDiceFragment() {
}
public static RollDiceFragment newInstance() {
RollDiceFragment fragment = new RollDiceFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rolldice, container, false);
random = new Random();
btnRoll = rootView.findViewById(R.id.btnRoll);
et_rollDice = rootView.findViewById(R.id.et_rollDice);
iv_dice1 = rootView.findViewById(R.id.iv_dice1);
iv_dice2 = rootView.findViewById(R.id.iv_dice2);
iv_dice3 = rootView.findViewById(R.id.iv_dice3);
iv_dice4 = rootView.findViewById(R.id.iv_dice4);
btnRoll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RollDiceFragment.this.rollDice();
}
});
return rootView;
}
private void rollDice(){
int d1 = this.random.nextInt(3) -1;
int d2 = this.random.nextInt(3) -1;
int d3 = this.random.nextInt(3) -1;
int d4 = this.random.nextInt(3) -1;
String print = (((BuildConfig.FLAVOR + addResult(d1)) + addResult(d2)) + addResult(d3)) + addResult(d4);
int diceSum = ((d1 + d2) + d3) + d4;
if (diceSum > 0){
this.et_rollDice.setText("+" + Integer.toString(diceSum));
}else {
this.et_rollDice.setText(Integer.toString(diceSum));
}
}
public String addResult(int i){
if (i == 1){
return "+";
}
if (i == 0){
return "0";
}
return "-";
}
}
The random roller works fine on the emulator and the result is shown in the editText line. I would just like the images to change based on the result of each dice. If a dice is showing a "-" the image shown should be diceminus.jpg.... Any help would be appreciated =)
The problem is that you are passing an int to setText method.
On the else case change it to using the setText overload with String as parameter:
this.et_rollDice.setText("" + Integer.toString(diceSum));
I found an answers so I will post it here in case somebody else needs the code one day =)
switch (d1){
case -1:
iv_dice1.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice1.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice1.setImageResource(R.drawable.diceplus);
break;
}
switch (d2){
case -1:
iv_dice2.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice2.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice2.setImageResource(R.drawable.diceplus);
break;
}
switch (d3){
case -1:
iv_dice3.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice3.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice3.setImageResource(R.drawable.diceplus);
break;
}
switch (d4){
case -1:
iv_dice4.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice4.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice4.setImageResource(R.drawable.diceplus);
break;
}

Java: Using created string as loop parameter?

In short, the user will input a number (say 1 through 3). This will decide which range of numbers the loop should search through.
switch(input){
case 1:
searchTerm = "i<10 && i>5";
case 2:
searchTerm = "i>=10 && i<19";
case 3:
searchTerm = "i>19 && i<24";
}
while(searchTerm){
//some function
}
Is this possible? I I've not been able to find a way to use a string as search parameters.
EDIT: I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters? For example:
case 1:
searchTerm = "i<5"
case 2:
searchTerm = "i>25 && i<29"
case 3:
searchTerm = "(i<50 && i>25) && (i>55 && i<75)"
case 4:
searchTerm = "(i<20 && i>15) && (i>300 && i<325) && (i>360 && i<380)
Then how does one do it? Multiple loops that call the same function?
The correct way to do this is to not use a string at all:
int min, max;
switch(input){
case 1: // i<10 && i>5
min = 6;
max = 10;
break; // to avoid follow-through to the next case
case 2: // i>=10 && i<19
min = 10;
max = 20;
break;
case 3: // i>19 && i<24
min = 20;
max = 25;
break;
default:
// You need something here in case the value entered wasn't 1-3
}
for (int i = min; i < max; ++i) {
// ...
}
Re your edit:
I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters?
In that case, you'll have to use an expression evaluator (or write one, which is a non-trivial task). There's one in Spring, for instance (not recommending, just happened to hear about it). A search for "Java expression evaluator" should turn up some options.
Another alternative, which is somewhat amusing given that some folks mistook your question for a JavaScript question, is to use the JavaScript evaluator built into Java (either Rhino or Nashorn). E.g.: Live Example
import javax.script.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String searchTerm = "i >= 19 && i <= 24";
int i;
try {
i = 19;
engine.put("i", i);
while ((boolean)engine.eval(searchTerm)) {
System.out.println("i = " + i);
++i;
engine.put("i", i);
}
System.out.println("Done");
} catch (ScriptException scriptException) {
System.out.println("Failed with script error");
}
}
}
...but you'll still have the problem of determining what initial value to use for i, which I've hardcoded above.
In Java 8 you can select a lambda instead of String:
Predicate<Integer> searchTerm = (Integer v) -> false;
switch (input) {
case 1:
searchTerm = (Integer v) -> v < 10 && v > 5;
break;
case 2:
searchTerm = (Integer v) -> v >= 10 && v < 19;
break;
case 3:
searchTerm = (Integer v) -> v > 19 && v < 24;
break;
}
while (searchTerm.test(i)) {
...
}
You can create an enumeration as below.
public enum SearchTerms {
None(""),
Between6And9("i<10 && i>5"),
Between10And18("i>=10 && i<19"),
Between20And23("i>19 && i<24");
private final String stringValue;
SearchTerms(String stringValue) {
this.stringValue = stringValue;
}
public String getStringValue() {
return stringValue;
}
public static SearchTerms fromStringValue(String stringValue) {
for (SearchTerms searchTerm : values()) {
if (searchTerm.getStringValue().equalsIgnoreCase(stringValue)) {
return searchTerm;
}
}
return SearchTerms.None;
}
}
Usage:
SearchTerms searchTerm = SearchTerms.fromStringValue("i<10 && i>5");
switch(searchTerm) {
case Between6And9:
//dosomething
break;
}
You can use .eval() of JavaScript.
Also don't forget break; at the end of each case:
Check out this fiddle.
Here is the snippet.
function test(input, i) {
switch (input) { //input=1
case 1:
searchTerm = "i<10 && i>5"; //this will be 'searchTerm'
break;
case 2:
searchTerm = "i>=10 && i<19";
break;
case 3:
searchTerm = "i>19 && i<24";
break;
}
while (eval(searchTerm)) { //'searchTerm' converted to boolean expression
alert(i); // alert for i=7,8,9
i++;
}
}
test(1, 7); //pass input=1 and i=7

Square of a number in Eclipse not working

This is the code for a calculator app for Android I'm making:
package com.example.calculator;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Stack;
import java.lang.Math;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.view.View;
import android.view.View.OnClickListener;
public class main extends Activity {
GridView mKeypadGrid;
TextView userInputText;
TextView memoryStatText;
Stack<String> mInputStack;
Stack<String> mOperationStack;
KeypadAdapter mKeypadAdapter;
TextView mStackText;
boolean resetInput = false;
boolean hasFinalResult = false;
String mDecimalSeperator;
double memoryValue = Double.NaN;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DecimalFormat currencyFormatter = (DecimalFormat) NumberFormat
.getInstance();
char decimalSeperator = currencyFormatter.getDecimalFormatSymbols()
.getDecimalSeparator();
mDecimalSeperator = Character.toString(decimalSeperator);
setContentView(R.layout.main);
// Create the stack
mInputStack = new Stack<String>();
mOperationStack = new Stack<String>();
// Get reference to the keypad button GridView
mKeypadGrid = (GridView) findViewById(R.id.grdButtons);
// Get reference to the user input TextView
userInputText = (TextView) findViewById(R.id.txtInput);
userInputText.setText("0");
memoryStatText = (TextView) findViewById(R.id.txtMemory);
memoryStatText.setText("");
mStackText = (TextView) findViewById(R.id.txtStack);
// Create Keypad Adapter
mKeypadAdapter = new KeypadAdapter(this);
// Set adapter of the keypad grid
mKeypadGrid.setAdapter(mKeypadAdapter);
// Set button click listener of the keypad adapter
mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button btn = (Button) v;
// Get the KeypadButton value which is used to identify the
// keypad button from the Button's tag
KeypadButton keypadButton = (KeypadButton) btn.getTag();
// Process keypad button
ProcessKeypadInput(keypadButton);
}
});
mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
private void ProcessKeypadInput(KeypadButton keypadButton) {
//Toast.makeText(this, keypadButton.getText(), Toast.LENGTH_SHORT).show();
String text = keypadButton.getText().toString();
String currentInput = userInputText.getText().toString();
int currentInputLen = currentInput.length();
String evalResult = null;
double userInputValue = Double.NaN;
switch (keypadButton) {
case BACKSPACE: // Handle backspace
// If has operand skip backspace
if (resetInput)
return;
int endIndex = currentInputLen - 1;
// There is one character at input so reset input to 0
if (endIndex < 1) {
userInputText.setText("0");
}
// Trim last character of the input text
else {
userInputText.setText(currentInput.subSequence(0, endIndex));
}
break;
case SIGN: // Handle -/+ sign
// input has text and is different than initial value 0
if (currentInputLen > 0 && currentInput != "0") {
// Already has (-) sign. Remove that sign
if (currentInput.charAt(0) == '-') {
userInputText.setText(currentInput.subSequence(1,
currentInputLen));
}
// Prepend (-) sign
else {
userInputText.setText("-" + currentInput.toString());
}
}
break;
case CE: // Handle clear input
userInputText.setText("0");
break;
case SQUARE:
double squareInput = Double.valueOf(currentInput);
userInputText.setText(squareInput+"*"+squareInput);
break;
case C: // Handle clear input and stack
userInputText.setText("0");
clearStacks();
break;
case DECIMAL_SEP: // Handle decimal separator
if (hasFinalResult || resetInput) {
userInputText.setText("0" + mDecimalSeperator);
hasFinalResult = false;
resetInput = false;
} else if (currentInput.contains("."))
return;
else
userInputText.append(mDecimalSeperator);
break;
case DIV:
case PLUS:
case MINUS:
case MULTIPLY:
if (resetInput) {
mInputStack.pop();
mOperationStack.pop();
} else {
if (currentInput.charAt(0) == '-') {
mInputStack.add("(" + currentInput + ")");
} else {
mInputStack.add(currentInput);
}
mOperationStack.add(currentInput);
}
mInputStack.add(text);
mOperationStack.add(text);
dumpInputStack();
evalResult = evaluateResult(false);
if (evalResult != null)
userInputText.setText(evalResult);
resetInput = true;
break;
case CALCULATE:
if (mOperationStack.size() == 0)
break;
mOperationStack.add(currentInput);
evalResult = evaluateResult(true);
if (evalResult != null) {
clearStacks();
userInputText.setText(evalResult);
resetInput = false;
hasFinalResult = true;
}
break;
case M_ADD: // Add user input value to memory buffer
userInputValue = tryParseUserInput();
if (Double.isNaN(userInputValue))
return;
if (Double.isNaN(memoryValue))
memoryValue = 0;
memoryValue += userInputValue;
displayMemoryStat();
hasFinalResult = true;
break;
case M_REMOVE: // Subtract user input value to memory buffer
userInputValue = tryParseUserInput();
if (Double.isNaN(userInputValue))
return;
if (Double.isNaN(memoryValue))
memoryValue = 0;
memoryValue -= userInputValue;
displayMemoryStat();
hasFinalResult = true;
break;
case MC: // Reset memory buffer to 0
memoryValue = Double.NaN;
displayMemoryStat();
break;
case MR: // Read memoryBuffer value
if (Double.isNaN(memoryValue))
return;
userInputText.setText(doubleToString(memoryValue));
displayMemoryStat();
break;
case MS: // Set memoryBuffer value to user input
userInputValue = tryParseUserInput();
if (Double.isNaN(userInputValue))
return;
memoryValue = userInputValue;
displayMemoryStat();
hasFinalResult = true;
break;
default:
if (Character.isDigit(text.charAt(0))) {
if (currentInput.equals("0") || resetInput || hasFinalResult) {
userInputText.setText(text);
resetInput = false;
hasFinalResult = false;
} else {
userInputText.append(text);
resetInput = false;
}
}
break;
}
}
private void clearStacks() {
mInputStack.clear();
mOperationStack.clear();
mStackText.setText("");
}
private void dumpInputStack() {
Iterator<String> it = mInputStack.iterator();
StringBuilder sb = new StringBuilder();
while (it.hasNext()) {
CharSequence iValue = it.next();
sb.append(iValue);
}
mStackText.setText(sb.toString());
}
private String evaluateResult(boolean requestedByUser) {
if ((!requestedByUser && mOperationStack.size() != 4)
|| (requestedByUser && mOperationStack.size() != 3))
return null;
String left = mOperationStack.get(0);
String operator = mOperationStack.get(1);
String right = mOperationStack.get(2);
String tmp = null;
if (!requestedByUser)
tmp = mOperationStack.get(3);
double leftVal = Double.parseDouble(left.toString());
double rightVal = Double.parseDouble(right.toString());
double result = Double.NaN;
if (operator.equals(KeypadButton.DIV.getText())) {
result = leftVal / rightVal;
} else if (operator.equals(KeypadButton.MULTIPLY.getText())) {
result = leftVal * rightVal;
} else if (operator.equals(KeypadButton.PLUS.getText())) {
result = leftVal + rightVal;
} else if (operator.equals(KeypadButton.MINUS.getText())) {
result = leftVal - rightVal;
}
String resultStr = doubleToString(result);
if (resultStr == null)
return null;
mOperationStack.clear();
if (!requestedByUser) {
mOperationStack.add(resultStr);
mOperationStack.add(tmp);
}
return resultStr;
}
private String doubleToString(double value) {
if (Double.isNaN(value))
return null;
long longVal = (long) value;
if (longVal == value)
return Long.toString(longVal);
else
return Double.toString(value);
}
private double tryParseUserInput() {
String inputStr = userInputText.getText().toString();
double result = Double.NaN;
try {
result = Double.parseDouble(inputStr);
} catch (NumberFormatException nfe) {
}
return result;
}
private void displayMemoryStat() {
if (Double.isNaN(memoryValue)) {
memoryStatText.setText("");
} else {
memoryStatText.setText("M = " + doubleToString(memoryValue));
}
}
}
To find the square of an inputted number, I use the following code:
case SQUARE:
double squareInput = Double.valueOf(currentInput);
userInputText.setText(squareInput+"*"+squareInput);
break;
But when the input the numbers 9 and 9, it just displays *9*9.* It doesn't display the output as 81.
Why is this, and can someone help me fix it?
You are converting number as String by adding "*". Instead you need is:
userInputText.setText(String.valueOf(squareInput*squareInput));
The way it works is:
First it sees 9 then you append a string "" which returns 9 and now you append another double to existing string which append 9 to existing string and thus you get "9*9"
Your formula is incorrect.
Try this.
double squareInput = Double.valueOf(currentInput);
userInputText.setText(String.format("%f", squareInput * squareInput));
You are setting the string value instead of square calcluated valued.
Do like this :
userInputText.setText(String.valueOf(squareInput*squareInput));
You ony concatinate the values a s a String. If you what the square you need to calculate and add it to the textfield:
userInputText.setText(squareInput+"*"+squareInput+"="+(squareInput*squareInput) );
Use the following code...It might be helpful for you..
case SQUARE:
double squareInput = Double.valueOf(currentInput);
userInputText.setText(String.valueOf(squareInput*squareInput));
break;
change your code to this
case SQUARE:
double squareInput = Double.valueOf(currentInput);
userInputText.setText(""+squareInput*squareInput);
break;

Plus or minus minor glitch in my android calculator

I have made an android calculator, and I have added the +/- button to it recently. The problems I am facing is, firstly when I use it, and press the equal button, the app crashes. Moreover as soon as I click +/- button, the digits turn into a float value, and when I type any number the number value continues after the decimal. Example: I have 200 and I press +/- the number becomes 200.0 and when I type any number it becomes 200.01. How do I overcome this problem? Can someone please let me know what are the changes required in my code? Here it is.
Thank you. :)
public class MainActivity extends Activity {
Typeface font1, font2;
TextView tv1;
private EditText Scr; //textbox screen
private float NumberBf; //Save screen before pressing button operation;
private String Operation;
private ButtonClickListener btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
font1=Typeface.createFromAsset(getAssets(), "digits.ttf");
Scr=(EditText)findViewById(R.id.editText);
Scr.setTypeface(font1);
font2=Typeface.createFromAsset(getAssets(), "alexbrush.TTF");
tv1=(TextView)findViewById(R.id.textView1);
tv1.setTypeface(font2);
Scr = (EditText) findViewById(R.id.editText);
Scr.setEnabled(false);
btnClick = new ButtonClickListener();
int idList[] = {R.id.button0,R.id.button7, R.id.button1, R.id.button8,R.id.button9,R.id.button4,
R.id.button5,R.id.button6,R.id.button,R.id.button2,R.id.button3,R.id.buttonDot,
R.id.buttonMul,R.id.buttonDiv,R.id.buttonAdd,R.id.buttonSub,R.id.buttonC,
R.id.buttonEq, R.id.buttonSqrt, R.id.buttonsquare, R.id.buttonNp
};
for(int id:idList){
View v = (View) findViewById(id);
v.setOnClickListener(btnClick);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void mMath(String str){
NumberBf = Float.parseFloat(Scr.getText().toString()); //save the screen
Operation = str; //save operation
Scr.setText("0"); //Clear screen
}
public void getKeyboard(String str){
String ScrCurrent = Scr.getText().toString();
if(ScrCurrent.equals("0"))
ScrCurrent = "";
ScrCurrent += str;
Scr.setText(ScrCurrent);
}
public void mResult(){
float NumAf = Float.parseFloat(Scr.getText().toString());
float result = 0;
if(Operation.equals("+")){
result = NumAf + NumberBf;
}
if(Operation.equals("-")){
result = NumberBf - NumAf;
}
if(Operation.equals("*")){
result = NumAf * NumberBf;
}
if(Operation.equals("/")){
result = NumberBf / NumAf;
}
Scr.setText(String.format("%10d", result));
}
public void fnSqrt(){
double Number = Double.parseDouble(Scr.getText().toString());
Number = Math.sqrt(Number);
Scr.setText(String.valueOf(Number));
}
public void fnSquare(){
float Number1 = Float.parseFloat(Scr.getText().toString());
Number1 = pow(Number1, 2);
Scr.setText(String.valueOf(Number1));
}
public void fnNp(){
float Number = Float.parseFloat(Scr.getText().toString());
Number = Number*(-1);
Scr.setText(String.valueOf(Number));
}
private float pow(float number1, int i) {
return number1*number1;
}
private class ButtonClickListener implements OnClickListener{
public void onClick(View v){
switch (v.getId()){
case R.id.buttonC: //Clear screen
Scr.setText("0");
NumberBf = 0;
Operation = "";
break;
case R.id.buttonAdd: //function Add
mMath("+");
break;
case R.id.buttonSub:
mMath("-");
break;
case R.id.buttonMul:
mMath("*");
break;
case R.id.buttonDiv:
mMath("/");
break;
case R.id.buttonEq:
mResult();
break;
case R.id.buttonSqrt:
fnSqrt();
break;
case R.id.buttonNp:
fnNp();
break;
case R.id.buttonsquare:
fnSquare();
break;
default:
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
}
}
Concerning your first problem: Do you get a nullpointer exception?
public void mResult(){
float NumAf = Float.parseFloat(Scr.getText().toString());
does not prevents your statement to try to turn a null String into a float which obviously will let your app crash. So include a mechanism to catch the null value before.
case R.id.buttonEq:
if (Scr.getText() != null)
mResult();
break;
I think this is the easiest way to prevent your mResult() from getting a null value.
For your second problem: It becomes a float because you do
float Number = Float.parseFloat(Scr.getText().toString());`
When you cast your 200 into a float it becomes 200.00 which will be returned as a string.
EDIT: I realized that you also have a dotButton (your code is really messy). The suggested alternative of course does not work with decimal numbers because it always casts your numbervalue. To prevent this behaviour maybe you can implement a simple if/else statement.
public void fnNp() {
if (Scr.getText().toString().contains(".")) {
float Number = Float.parseFloat(Scr.getText().toString());
Number = Number*(-1);
Scr.setText(String.valueOf(Number));
} else {
int number = Integer.parseInt(Scr.getText().toString());
Number = Number*(-1);
Scr.setText(String.valueOf(Number));
}
}
You need to do a if statement in mResult that checks whether the float has a absolute difference to a float of the int of the float , less than , say Float.MIN_VALUE , and then get the toString of the int of the float instead if it is.
Wrap the call Scr.setText( Float f) , in a function setScreenTextWithNumber(Number n)
and do the checking there.
If you just check for the decimal point, then 2.0000 / 2 = 1.000 if that is what you want.
Otherwise checking for significant differences makes a call that a number that is not significantly different from the nearest integer is an integer.

Categories