Divide two values obtained from EditText ad divide them by each other - android

Is it possible in Java code to divide two obtained values from two EditText boxes in an android app, and then divide them by each other to create a result?
Pretty much, the consumer of my application will be asked for two DIFFERENT, numerical values to be inputted into each box.
What I want to do with each value is produce a mathematical sum.
E.g.
If the first EditText box contains "22"
and the second EditText contains "11"
I want to be able to take those two values and divide them by each other to produce a value which I can further use.
In this case, that value produced would be 2.
22 / 11 = 2
I've already imported these two values into another Class (labeled DataHelper) through className.insert(stringName).
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
// TODO Auto-generated method stub
}
}
That's the Class for DataHelper with the two String values.
I have tried using IEEEremainder(x, y), but I don't know exactly how to use it since I am a novice at this language.
Can anyone give me some help with this? Any help at all will be appreciated.
Regards,
Mike.

if under standing rigtig you cut due
int one = int value1 / int vlaue2;
but then when you get the value from a edittext you get a string so you need to convert it to a int first int a = new Integer("value from edittext").intValue();

I believe you want something like this:
public class DataHelper extends Activity{
public static void insert(String firstFB, String firstRL) {
int fb = Integer.parseInt(firstFB);
int rl = Integer.parseInt(firstRL);
int division = fb / rl; // Or use double if needed
}
}

Related

Is there some pattern on Random() method?

I started making a project where there are goats! Yeah Goats.
Currently there is only one function, when I click a goat, it create another goat in a Random position.
I realized that there is a pattern of positions:
Here is the code:
public class GameActivity extends Activity {
private int[] arrGoats = new int[5];
private RelativeLayout battlefield;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
battlefield = (RelativeLayout) findViewById(R.id.rel_battlefield);
arrGoats[0] = R.drawable.amarelo;
arrGoats[1] = R.drawable.azul;
arrGoats[2] = R.drawable.branco;
arrGoats[3] = R.drawable.verde;
arrGoats[4] = R.drawable.vermelho;
criarCabra(60, 100);
}
private void criarCabra(float x, float y) {
int cabraImg = arrGoats[new Random().nextInt(4)];
ImageView cabra = new ImageView(this);
cabra.setImageResource(cabraImg);
cabra.setX(x);
cabra.setY(y);
LayoutParams params = (LayoutParams) new LayoutParams(MarginLayoutParams.WRAP_CONTENT,
MarginLayoutParams.WRAP_CONTENT);
params.width = 150;
params.height = 120;
cabra.setLayoutParams(params);
cabra.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
criarCabra(new Random().nextInt(2000), new Random().nextInt(1000));
}
});
battlefield.addView(cabra);
}
}
I would like to know why this pattern is being created although I'm using Random().NextInt() to define goats positions.
Am I crazy?
First, you're creating a new Random object each time. In Android, the initial seed is derived from current time and the identity hash code:
public Random() {
// Note: Using identityHashCode() to be hermetic wrt subclasses.
setSeed(System.currentTimeMillis() + System.identityHashCode(this));
}
For two objects created in sequence, the identity hash codes are close to each other. On my Android KitKat Dalvik VM, I get identity hash codes that differ only by 32.
The currentTimeMillis() does not provide much difference to the seeds either.
The random itself is a linear congruential generator of the form
random[i+1] = a * random[i] + b (mod c)
where random[0] is the seed and a, b and c are parameters.
Based on this answer, similar seeds indeed produce similar results in linear congruential generators:
The reason you're seeing similar initial output from nextDouble given similar seeds is that, because the computation of the next integer only involves a multiplication and addition, the magnitude of the next integer is not much affected by differences in the lower bits.
Hence, your two successively generated Randoms with default seeds will produce values that seem to be correlated and make your goats get positioned on a line.
To fix it, use the same Random object and/or a more random pseudorandom generator than a linear congruential one.
You are creating new instances of Random with every call to criarCabra and every invocation of onClick. Create a single static instance of Random, then re-use it.
Unless you really know what you're doing and have a very good reason to be doing it, the best practice is to only create one instance of Random per program and then poll it whenever you need additional values.

Android: how to multiply values from EditText?

Ok, first thing is that I am new in Android development, please do not shoot me for the question.
So, I am developing an app yhat needs to multiply from 3 EditTexts:
resultsEditText
amountEditText
taxEditText
They are set with the same name in the R.java. what I want is the following:
amountEditText * taxEditText = resultsEditText
I have no idea in how to implement this, I have searched the internet and this site, which I use as a reference for all my Android development needs, and all the code I found doesnt work at all. I dont know what else to do. Thanks in advance!
You need to set EditText input type as number as well so user can input only numbers.
int a = Integer.parseInt(resultsEditText.getText().toString().toTrim());
int b = Integer.parseInt(amountEditText.getText().toString().toTrim());
int c = Integer.parseInt(taxEditText.getText().toString().toTrim());
int result = a * b * c;
Button btnCal,btnSub;
EditText editLen,editWid,editFeet;
int a,b,res;
btnSub=(Button)findViewById(R.id.btnSub);
btnCal=(Button)findViewById(R.id.btnCal);
editLen=(EditText)findViewById(R.id.editLen);
editWid=(EditText)findViewById(R.id.editWid);
editFeet=(EditText)findViewById(R.id.editFeet);
btnCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.parseInt(editLen.getText().toString());
b = Integer.parseInt(editWid.getText().toString());
res=a*b;
editFeet.setText(String.valueOf(res));
}
});

is there anyway i can have listarray<string,int,int> in android?

is there anyway i can have multi dimensional array ex: listarray string,int,int in android??
it is quite possible with Map & Set in Java/C++.
Make your own container with fields good explaining its purpose:
class PersonData {
public int age;
public int id;
public String name }
And make list of it:
List<PersonData> dataList = new ArrayList<PersonData>();
Acces your fields by:
dataList.get(5).age = 11;
As Egor said in comment, a good practice will be set those field as protected and create setters and getters, if you don't need extreme performance in this specific case.

String replace in Android

To try string replace in Android, I wrote a small snippet:
public class cs{
public static void main(String[] args){
String a,c;
int b;
b=1;
c="12345";
a="12345,54321";
a.replace(c,String.valueOf(b));
System.out.println(a);
}
}
Expected Output: 12345,54321 changes to 1,54321
Actual Output: 12345,54321
。Please help。
Is the . in c.String.valueOf(b) a typo for a comma, separating two parameters? Because it doesn't make much sense as stated. replace accepts two parameters, and furthermore, it doesn't change the string it's executed on, it merely returns a new one, so you need to pick up that return value and reassign it to the variable:
a = a.replace(c, String.valueOf(b));
If you expect output 1,54321 you need to write
a.replace("12345", c.String.valueOf(b));

How to create an object from UI elements in a nice way?

In my android application I have some fields like
MyButton a;
MyEditText b;
MySpinner c;
MyTextView d;
Each of these is like public class MyButton extends Button implements InfoExtract and
public interface InfoExtract{
String getTheText();
}
Out of these fields (the user can change some of them), there is a UserProfile to be created like so:
public class ProfileUpdate {
// The fields have to be string no matter what
String firstName;
String lastName;
String dateOfBirth;
String relationshipStatus
}
The execution flow is like that:
List<InfoExtract> uiElements = new ArrayList<InfoExtract>();
uiElements.add(a);
uiElements.add(b);
uiElements.add(c);
uiElements.add(d);
someButton.setOnClickListener(new SaveProfileListener(uiElements);
The SaveProfileListener more or less does this:
ProfileUpdate pup = new ProfileUpdate();
int i = 0;
pup.firstName = uiElements.get(i++).getTheText()
pup.lastName = uiElements.get(i++).getTheText();
pup.dateOfBirth = uiElements.get(i++).getTheText();
pup.relationshipStatus = uiElements.get(i++).getTheText();
Bad thing #1: If I want to add another fields its obviously much work.
Bad thing #2: The order of elements in the list is important.
Bad thing #3: I cannot easily manipulate the date format for dateOfBirth for example.
What i could have done but what is equally bad:
// Pass the listener what fields it should use.
someButton.setOnClickListener(new SaveProfileListener(a, b, c, d);
How to make this nice and clean?
I ended up using a Map and a new Enum type for each field.

Categories

Resources