Android, Reading blank values - android

I'm new to android development, I trying to create a simply Pythagorean Calculator, I need help with reading if a lines blank, but still calculates instead of failing.
Here is my code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText sideAObj;
private EditText sideBObj;
private EditText sideCObj;
private EditText outputObj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sideAObj = (EditText) findViewById(R.id.SideAInput);
sideBObj = (EditText) findViewById(R.id.SideBInput);
sideCObj = (EditText) findViewById(R.id.SideCInput);
outputObj = (EditText) findViewById(R.id.OutputText);
}
public void calculateClick(View v){
try {
double sideA = Double.parseDouble(sideAObj.getText().toString());
double sideB = Double.parseDouble(sideBObj.getText().toString());
double sideC = Double.parseDouble(sideCObj.getText().toString());
if (sideAObj.getText().toString().equalsIgnoreCase("0")) {
double pt = Math.sqrt((sideC * sideC) - (sideB * sideB));
outputObj.setText(String.format("%.2f", pt));
}
}
catch (NumberFormatException ex){
Toast errMess = Toast.makeText(getApplicationContext(),"Enter Numbers Only",Toast.LENGTH_SHORT);
errMess.show();
outputObj.setText(String.format("%2.f",0.00));
return;
}
}
public void clearClick(View v){
sideAObj.setText("");
sideBObj.setText("");
sideCObj.setText("");
outputObj.setText("");
sideAObj.requestFocus();
}
}
My program will calculate if their is a Zero on 1 line, but if I leave it blank the program fails entirely, whats the best way to prevent that.

It will obviously fail as it doesn't know how to parse a blank value into a double. Just use something like this during instantiation itself:
double sideB = (sideBObj.getText().toString() == "") ? 0 : (Double.parseDouble(sideBObj.getText().toString()));
double sideC = (sideCObj.getText().toString() == "") ? 0 : (Double.parseDouble(sideCObj.getText().toString()));
Basically, you will be assigning the value 0 if the edit text field is 0 else, you will parse the value entered to a double.
Assuming you want to consider a 0 if there is a blank edit text field.
========================================================================
UPDATE
if(sideAObj.getText().toString() != ""){
double sideA = Double.parseDouble(sideAObj.getText().toString());
}

The simple solution for this problem would be to check each edittext whether they are blank or not and then perform the task.
Get the value of each Edittext to a int variable and then use loop and with the help of edittext.length() method verify if it is equal to 0, if yes, then assign a value to 0 to a new global variable, else assign the exact value to global variable.
and then perform the calculation with the new variables.
Sample code for better understanding :-
String a = et.getText().toString();
int l = a.length();
if (l == 0){
// set the value of global variable = 0;
} else {
// set the value of global variable = a {Actual Digit}
}

Related

Operater '*' cannot be applied to 'android.widget.RadioButton', 'int'

Operater '*' cannot be applied to 'android.widget.RadioButton', 'int'
I'm creating an app that is for the cost of printing paper using radio buttons with a 20 paper limit.
I'm unsure as to why i'm getting this error as i'm grabbing getting the information from my EditText.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
double totalcost;
double price1 = 0.15;
double price2 = 0.45;
double price3 = 0.80;
int prints;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setLogo(R.mipmap.ic_launcher);
getActionBar().setDisplayUseLogoEnabled(true);
final EditText prints =(EditText)findViewById(R.id.noprint);
final RadioButton price1 = (RadioButton)findViewById(R.id.rad1);
final RadioButton price2 = (RadioButton)findViewById(R.id.rad2);
final RadioButton price3 = (RadioButton)findViewById(R.id.rad3);
final TextView totalcost =(TextView)findViewById(R.id.cost);
Button calculate =(Button)findViewById(R.id.btn1);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String prints = noprint.getText().toString();
int prints = Integer.parseInt(noprint);
DecimalFormat tenth = new DecimalFormat("$##.##");
if (price1.isChecked()) {
if (prints <= 20) {
totalcost = price1 * prints;
} else {
Toast.makeText(MainActivity.this, "Number of prints must be less than 20", Toast.LENGTH_LONG).show();
}
}
Looking at your program, you have a naming problem. On class-level, you define an attribute
double price1 = 0.15;
but within method onCreate(...) you also define
final RadioButton price1 = (RadioButton)findViewById(R.id.rad1);
which effectively hides the attribute. Thus, when you write
totalcost = price1 * prints;
You command java to multiply a RadioButton with an int. Since this binary operator (*(RadioButton, int)) is undefined in Java, you get the compiler error you got. Your intention is probably to use the attribute price1. A quick fix would be to expicitly use the attribute through the this reference:
totalcost = this.price1 * prints;
A cleaner fix would be to rename your variables so you do not hide the attribute with the local variable.

Calculator based on one field

I'm working on Android Studio, to make one field calculator
It will be just one Text field, button, and another text view.
If I put "2+5*6" for example it must understand the operation.
Can anyone help me?
Check out my code please
public class MainActivity extends AppCompatActivity {
public static final String equation = "[0-9]";
String opr = " ";
int[] result;
int castedInt;
String temp;
String[] separated;
EditText txte;
TextView txtv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buResult(View view) {
txte = (EditText)findViewById(R.id.editText);
for (int i = 0; i < txte.length(); i++) {
if (Pattern.matches(txte.toString(), equation)) {
separated[i] = txte.toString();
temp = separated[i];
castedInt = Integer.parseInt(temp.toString());
result[i] = castedInt;
}
else {
opr = txte.toString();
}
txtv.setText(result[i] + opr + result[i]);
}
}
}
You should implement reverse polish notation for string.
Example code could be find here https://codereview.stackexchange.com/questions/120451/reverse-polish-notation-calculator-in-java
As defined in the documentation regex must be a regular expression. You need to transform the user input into a regular expression.
Pattern.matches(String regex, CharSequence input)
Also you are always using the whole user input:
separated[i] = txte.toString();
All "separated" indexes will contain the same.
If it's Ok to use language support (instead of creating algorithm from scratch) you can use like this:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
...
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
txtv.setText(engine.eval(txte.toString()));
*credits : stackoverflow.com/a/2605051/936786

How to save number from user input?

Recently I've started to mess around with Android Studio and decided to make an app. I've made the most of my app, but I encounter a little problem. I need to memorize in a variable a number from user input, but I don't know how to that, I've tried solutions found on the internet, even here but I get an error.
Can somebody help me with some ideas or the code edited which I must put in my app ?
This is the java code for the activity:
public class calc_medie_teza extends ActionBarActivity implements View.OnClickListener {
EditText adaug_nota;
static final int READ_BLOCK_SIZE = 100;
TextView afisare;
TextView afisare2;
Button calc;
EditText note_nr;
EditText nota_teza;
int suma;
double medie;
double medieteza;
int nr_note = 0;
int notamedie;
int notateza;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc_medie_teza);
afisare = (TextView) findViewById(R.id.afisare);
afisare2 = (TextView) findViewById(R.id.afisare2);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(this);
note_nr = (EditText) findViewById(R.id.note_nr);
nota_teza = (EditText) findViewById(R.id.nota_teza);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calc_medie_teza, menu); // creare meniu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); // meniu
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public void onClick(View v) // afisare medie
{
calcul_medie();
}
public void buton_adauga(View v)
{
if(note_nr == )
suma = suma + notamedie;
nr_note = nr_note + 1;
}
public double calcul_medie() // calculul mediei
{
medie = suma / nr_note;
medieteza = ((medie * 3)+ notateza)/4;
return medieteza;
}
Here is a photo with the activity: http://onlypro.ro/img/images/ootllv2f55hnwdgi0xlv.png
Basically the app needs to add the input number to variable when I press the "Adauga nota" [Add grade] button and then I have to insert the "teza" number and when press the "Calculeaza media" [Calculate] the app will then calculate the calcul_medie() and return a decimal number. Between "Introduceti notele aici" [Add grades here] and "Adaugata nota"[Add grade] I have a Number enter text, the same is between "Introduceti teza aici" [Add thesis bere] and "Calculeaza media" [Calculate]. I don't know how to store the number the user puts in and sum it in "notamedie" variable.
I hope you understand my problem.
For any questions you'll have I'll respond as soon as I can.
Thanks in advance !
Well, I think your probably asked how to get the input of the String from the UI and translate them into Integer/BigDecimal. If so, here is some solution:
first, you get get the string from the UI:
String input = note_nr.getText().toString().trim();
change the string input to integer/BigDecimal:
int number1 = Integer.parseInt(input);
BigDecimal number2 = new BigDecimal(input);
Correct me if misunderstood your questions. Thanks.
I did not understand the problem at all. Please clearly define the problem and use english if you can. As fas the I understood there will be a Edittext to which the user will input the value. Gets its value in the activity and then use it.
EditText edittext;
editext = (EditText) findViewById(R.id.editext);
String value = edit text.getText().toString(); // the value that the user inputted in String data format
// Here for addition you will need the value in int or decimal for that
int valueInt = Integer.parse(value); // this will be the value in int type
double valueInt = = Double.parseDouble(value); // this will be the value (user inputted) in double data type ( which you will need if you want to use decimal numbers).
When you press a button just retrieve the appropriate value from the edittext in the following way
edittext = (EditText)layout.findViewById(R.id.txtDescription);
String string = edittext.getText().toString();
to retrieve the text do this
String string = note_nr.getText().toString();
// Converting String to int
int myValue =Integer.parseInt(string);
I tried that one more time, but the IDE says that the variable I used for "string" is never used, but I've used it.
number1= (EditText) findViewById(R.id.number1);
String number_1 = number1.getText().toString();
number2= (EditText) findViewById(R.id.number2);
String number_2= number2.getText().toString();
R3muSGFX, can you post the whole codes?
String number_1 = number1.getText().toString(); this line codes just mean that you initialized a String variable "number_1".
and you will used it when app perform a math function, like you wrote in the beginning:
public double calcul_medie()
{
medie = suma / nr_note;
medieteza = ((medie * 3)+ notateza)/4;
return medieteza;
}
but if you did not use "number_1" at all, IDE will imply you the warning message
:the variable I used for "string" is never used

Value of 1 array equals the value of another in Android

I have two arrays :
String []myExpressions = {"20+10","50+50","25+25","10+15"};
String []answers = {"30#","100#","50#","25#"};
When the user clicks the generate button it generates an expression from the array myExpressions and displays it in text-field. Then I require the user to enter the answer using the buttons provided. The answer is displayed in a EditText. When the user enters an answer they should enter a #(like a submit button) and if is the correct answer it should display correct in a text-field. So if the position in the expression array is 1, the correct answer is in the answer array in the same position. How would i check if they are in the same position?
For example: myExpressions[1] correct answer to this is answers[1].
Here is my code:
package com.gamesup.braingame;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Easy extends Activity implements OnClickListener{
EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
final String []myExpressions = {"20+10","50+50","25+25","10+15"};
final String []answers = {"30#","100#","50#","25#"};
final TextView displayExpression = (TextView) findViewById(R.id.expression);
Button generate = (Button) findViewById(R.id.random_gen);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(4) ;
displayExpression.setText(myExpressions[random]);
}
});
}
static boolean isEmpty = true;
public void num_Clicked(View v){
Button btn = (Button) findViewById(v.getId());
//getting the button object and using a view to get the id of the buttons
if (v.getId()== R.id.del_button){
String s = display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
// storing the existing number into editText and current text is set
//to current button value
//display.setText(number+btn.getText().toString());
//the total in the editText
}
if (v.getId()== R.id.hash_button){
String userAns = display.getText().toString();
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
For starters arrays in Java Begin at Index 0 so therefore , to compare the first items of the Array you should be using something like this to check if things are equal:
EditText myAnswer = (EditText) findViewById(R.id.myAnswer);
String answer = myAnswer.getText().toString();
// Notice my loop starts at 0 because the first index of an array is 0
for(int i = 0 ; i < answers.length; i++)
{
if(answers[i].equals(answer))
{
// Congratulate the User for getting it Right
}
}
It seems as though you have a little bit of a shaky logic. IMHO you should be using a multidimensional Array.
With a multidimensional Array you can essentially set up keys and values.
This is how I think your application should be configures
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5", "9"},
{"20 * 3","60"},
{"99 - 9","90"}};
// Fetch your random question, since we know our questions are the first item in out Array we can use the index [x][0] depending on the index you pull from
String question = multiArray[0][0];
// To do this randomly
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(4) ;
String question = multiArray[random][0];
// Get the Answer from yout EditText
String answer = myAnswer.getText().toString();
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(mutliArray[i][1])
{
// We have found the answer, Congratulate the User
}else{
// Tell them how bad they are since they can't solve simple equations!
// ....joking obviously we would be nice and let them know its not the answer
}
}
In this line
int random = ranGenerate.nextInt(4) ;
why don't you make random an instance variable inside your class? This way you would preserve the index, and you would know which index to use to compare the answer.

Output not able to show at UI

I'm currently trying to write an app to calculate BMI and calories needed by a person (male / female).
My apps have 2 parts:
1. BMI calculation
2. Calories needed
The first part works well (so I excluded the code for this part), but for the 2nd part, calories needed calculation, it is not able to show up the result as expected (after I click on 'calories needed' button). Probably something is still missing but I cant find it so far.
Everything looks fine in the code, no error.
Can anyone help to have a look on it? :) thanks in advance.
Code as below:
package com.example.caloriescalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
public class BMIcalculation extends Activity
{
EditText weightE;
EditText heightE ;
EditText ageE ;
TextView caloriesresult ;
RadioButton male;
RadioButton female;
Button calories;
EditText weightText ;
EditText heightText ;
EditText ageText;
TextView resultText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmilayout_main);
weightE = (EditText)findViewById(R.id.weightText);
heightE = (EditText)findViewById(R.id.heightText);
ageE = (EditText)findViewById(R.id.ageText);
caloriesresult = (TextView)findViewById(R.id.caloriesText);
male = (RadioButton) findViewById(R.id.maleradio);
female = (RadioButton) findViewById(R.id.femaleradio);
Button calories = (Button) findViewById(R.id.caloriesButton);
calories.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
int caloriesneed = 0, weight = 0, height = 0, age = 0;
try {
if ((!weightE.equals("")) && (!heightE.equals("")) && (!ageE.equals("")))
{
weightE = (EditText) findViewById(R.id.weightText);
weight = Integer.parseInt(weightE.getText().toString().trim());
heightE = (EditText) findViewById(R.id.heightText);
height = Integer.parseInt(heightE.getText().toString().trim());
ageE = (EditText) findViewById(R.id.ageText);
age = Integer.parseInt(ageE.getText().toString().trim());
if (male.isSelected())
{
caloriesneed = (int) Math.round (655 + 9.6*weight + 1.8*height - 4.7*age);
caloriesresult.setText(caloriesneed);
}
else if (female.isSelected())
{
caloriesneed = (int) Math.round (66 + 13.7*weight + 5*height - 6.8*age);
caloriesresult.setText(caloriesneed);
}
}
}
catch (Exception k)
{ System.out.println(k);
}
}
});
}
caloriesresult.setText(caloriesneed);
This is problem. In Android if you assign pure int variable to some TextWidget, error will be thrown because Android will interpret it as resouce id of stored String in strings.xml.
So for this reason you need explicit casting. You can achieve it with concentation or an usage of String.valueOf(int value) or Integer.toString(int value) method:
textWidget.setText(Integer.toString(value)); // best approach
textWidget.setText(String.valueOf(value));
textWidget.setText("" + value);
Note: This is absolutely normal behaviour since if you are developing comercial application, usually you want to have support for more languages - and this feature requires localised Strings stored in proper XML file(s).
I guess it's because you try to input an integer to settext() which requires a string for input parameter, so you can try:
caloriesresult.setText("" + caloriesneed);
or
caloriesresult.setText(Integer.toString(caloriesneed));
or
caloriesresult.setText(String.valueOf(caloriesneed));

Categories

Resources