• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to calculate? Please kindly help me

Leye

Lurker
Jul 22, 2016
4
0
I want to create app. I have attached the layout.

HTML:
<p><b>My codes are:</b></p>

Java:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        //This is where my codes will go
       
        //linking EditText widgets
       
        final EditText eAss1 = (EditText)findViewById(R.id.ass1);
        final EditText eAss2 = (EditText)findViewById(R.id.ass2);
        final EditText eAss3 = (EditText)findViewById(R.id.Ass3);
        final EditText eExamMark = (EditText)findViewById(R.id.ExamMark);
       
        //linking TextView widgets
       
        final TextView errorMessage = (TextView)findViewById(R.id.errorView);
        final TextView efinalPerce = (TextView)findViewById(R.id.finalPerc);
        final TextView efailOrPass = (TextView)findViewById(R.id.failOrPass);
       
        //linking the button widget
       
        Button but1 = (Button)findViewById(R.id.btnCalc);
        but1.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
               
                //this is where my codes for calculations will go
                double eAss1Val = Double.valueOf(eAss1.getText());
                double eAss2Val = Double.valueOf(eAss2.getText());
                double eAss3Val = Double.valueOf(eAss3.getText());
                double eExamMark = Double.valueOf(eExamMark);
           
                double yearMark = eAss1Val * 0.15 + eAss2Val * 0.35 + eAss3Val * 0.5;
                double finalMark = yearMark * .49 + eExamMark * .51;
               
               
               }//end of onClick
        });

HTML:
<p>But am getting error saying</p>

<p>The method valueOf(string) in the type double is not applicable for the arguments(Editable)

<p>examMark.getText() cannot invoke getText() on the primitive type double</p>

<p>Please, someone kindly assist me on how to do the calculation of the layout I attached. </p?

<p>Thanks</p>
 

Attachments

  • WP_20160722_14_37_04_Pro.jpg
    WP_20160722_14_37_04_Pro.jpg
    246.2 KB · Views: 268
Wow, you are *really* going to struggle if you don't understand what's going wrong here.
My advice is to get a good book on Java. Read it and learn the basics of the language. Or follow an online tutorial, which explains about basic types and their equivalent object counterparts.

If you had any understanding of the Double class, you would know that there are methods on this class which are used to create instances of Double, and convert to its primitive type equivalent - double.

The valueOf() method takes a String. Rather than just giving up and asking basic questions, try to work out why this is causing a problem. Hint: what type is 'eExamMark'?

And the lines of code preceding it (which I gave you) should give you a big hint.

Code:
double eExamMark = Double.valueOf(eExamMark);

As this is clearly a homework assignment, you should try to learn, rather than just ask for spoon-fed answers on a forum.
 
Last edited by a moderator:
  • Like
Reactions: Bg260 and mikedt
Upvote 0
At the beginning of 2016, I bought a book on Java, it was 2nd hand, cheap and nasty and full of mistakes and deprecated classes and methods. But I figured it out eventually. Hope this helps:

package java101;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WorkWithFloating {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("Enter radius: ");

String text = input.readLine();
Double x = new Double(text);
double r = x.doubleValue();
// double r = new Double(text).doubleValue(); // another way of coding it.
/*
* Difference between variables e.g. double and objects e.g. Double
* Object-oriented programming : the choice of objects used in a program is based on the operations that have to be performed
* 1. Input a floating point number;
* 2. apply the formula A = PI*r*r to it;
* 3. output the result
* =================================================================
* x is a variable of the Double CLASS
* r is a variable of type double
* They both represent the real number 100.0, the reason that we have these two separate objects
* representing the same thing is because of the different operations we have to perform on the value!
* As an instance of the Double CLASS, the object x can obtain its value from the text object which holds the value
* ... 100 in four characters '1', '0', '0' and '\n'.
* These flowed in from the reader object, which held the value 100 in the four bytes 49, 48,48 and 13.
* So the x object has the ability to convert a numeric value from its character representations into a numeric representation.
* But implementing the formula A=PI*r*r requires the use of the multiplication operator *, which only works on numeric
* variables, not objects, so the object x has to be converted to the variable r using the doubleValue() method on x.
*/
System.out.println("Area of the circle with radius " + r);
double area = Math.PI*r*r;
System.out.println(" is " + area);


}

}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones