All variables in app/java/com.(the project name)/MainActivity must be defined. Define global variables at the start of the class, before the "onCreate" method. Casual variables only used within a method can be defined within the method. The useful number types are double (real) and int (integer). Here are definitions for two variables called "realFld" and "intfld".
double realFld = 0.0;
int intfld = 0;
If you want a user to enter a number:
In app/res/layout/activity_main.xml, code an EditText and a Button:
android:inputType="numberDecimal" is very useful, as it provides a numeric keyboard, and stops non-numerics being entered. Click here for more info about formatting the screen in app/res/layout/activity_main.xml.
Move to app/java/com.(the project name)/MainActivity and code the method "doClear":
public void doClear(View view) {
EditText oz = (EditText) findViewById(R.id.ozVal);
oz.setText("");
}
This means that if a user taps the field, it will clear it to make entering something easier.
Coding this will cause the error "Cannot resolve symbol EditText". Click on EditText, then do Alt/Enter.
"findViewById" enables the code to find the data. "R.id.ozVal" says which field you're using. "oz" merely ties the "findViewById" to the "setText", which does the actual outputing of the field. Yes, I know we're inputting a field, but this clearing business is output. Right? The input itself will actually be done with the button.
The button code (above) specifies the method "doCalc", so we must code that. What comes in must be converted to a string. No, I don't know what it is. but this will convert it:
public void doCalc(View view) {
EditText oz = (EditText) findViewById(R.id.ozVal);
String ozInput = oz.getText().toString();
// code to actually do something with this input
}
But we want a number, not a string. Also, while the EditText did specify android:inputType="numberDecimal", it would be good practice to check that this really IS a number. Here is a useful method which takes a string (the "ozInput" in the method "doCakc", for example) and converts it to a double (a real number). If it isn't a proper number, this traps the exception, and returns a zero, instead. You could do something else, if you prefer.
double ozAmt = convNum(ozInput);
// trap non-numerics in input string
private double convNum(String inp) {
try{return Double.parseDouble(inp);}
catch(Exception e){return 0;}
}
Calculation and conversion
Converting a string to a double is shown above. Converting a double to a (formatted) string is shown below.
Rounding - both "gmAmt" and "ozAmt" must be real (double):
gmAmt = Math.round(ozAmt * 28.35);
Extracting the decimal part of a number:
decimal = fld - Math.floor(fld);
Making the integer "intFld" into a double called "realFld" (I've defined realFld at this point, but you don't have to):
double realFld = (double) intFld;
Making double called "realFld" into an integer "int2Fld":
int int2Fld = (int) realFld;
It might be a good idea to truncate (using Math.floor()) or round (using Math.round()) first!
Arithmetic - please note - this is IMPORTANT! Java tends to do either integer or real arithmetic. If it is doing integer arithmetic, then it truncates as it goes. And it does integer arithmetic if the numbers are integers. So 5/2 will produce 2, not 2.5, EVEN if you assign the result to a real. Grr!!! The way round this, if you really want real arithmetic, is to make all your numeric constants into reals. 5.0/2.0 will give you 2.5.
Otherwise, arithmetic is similar to most programming languages.
If you want a number to be output to the screen:
In app/res/layout/activity_main.xml, code a TextView: Click here for more info about formatting the screen in app/res/layout/activity_main.xml.
Move to app/java/com.(the project name)/MainActivity and code something like this in an appropriate place:
String gmOut = String.format("%4.2f", fld);
TextView gm = (TextView) findViewById(R.id.gmVal);
gm.setText(gmOut);
"fld" is what you want to output. It is a double (a real number). However, you can only output a text field, not a double. "String.format" formats "fld" with 2 decimal places and (at most) 4 digits before the decimal point. This result will be a string, which is what we want. We are using TextView rather than EditTExt because gmVal is a TextView.
Random numbers
Random numbers are a class in Java, so before you use them, you need to set up an object. This is only done once. It is a definition, not a bit of code, so can be done at the start, before any methods. (These starting definitions I call global variables, which is NOT a Java term. But that's what they are!)
static Random myRand = new Random();
When you want an integer random number, this puts a value, 0 <= randomInt < range, into the variable 'randomInt' (or anything else you want to call it).
int randomInt = myRand.nextInt(range);
When you want a double random number, this puts a value, 0.0 <= randomDouble < 1.0, into the variable 'randomDouble' (or anything else you want to call it).
double randomDouble = myRand.nextDouble();
It should give a DIFFERENT sequence each time the app is run.