Charles Carrington
Charles Carrington

Reputation: 111

New to Java and having issues with which name/variable references

I've used this site searching for specific errors for two semesters now. I know the question I am asking is more broad and basic than most of the things I've searched/read on this site, but this is the only place I have to turn for information outside of the classroom.

Right now I'm working on a simple Photo Calculator Phone application using Android Studio. It has 3 radio buttons with each assigned a specific value. I know there is something wrong with how I'm referencing each of the buttons and attempting to trigger a calculation.

Some of the current errors:

    Cannot resolve method'getText()'
    Operator '<' cannot be applied to 'android.widget.editText','int'
    Operator "*" cannot be applied to 'double', 'andriod.widget.EditText'

Below is the code:

        package com.example.squirreloverlord.ccarringtonphonephotoprint;

    import android.icu.text.DecimalFormat;
    import android.support.v7.app.AppCompatActivity;
    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;

    public class MainActivity extends AppCompatActivity {
    double small = 19;
    double medium = 49;
    double large = 79;
    double Result;
    double inputUser;
    double Number;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);

        final EditText inputUser = (EditText)     findViewById(R.id.editTextNumber);
        final RadioButton radioButton4x6 = (RadioButton)     findViewById(R.id.radioButton4x6);
        final RadioButton radioButton5x7 = (RadioButton) findViewById(R.id.radioButton5x7);
        final RadioButton radioButton8x10  = (RadioButton)findViewById(R.id.radioButton8x10);
        final TextView Result = (TextView) findViewById(R.id.textViewResult);
        Button Calculate = (Button) findViewById(R.id.buttonCalculate);

        Calculate.setOnClickListener(new View.OnClickListener()  {
            @Override
            public void onClick(View v)  {
                inputUser=Double.parseDouble(Number.getText( ).toString(  ));
                DecimalFormat tenth = new DecimalFormat("#.#");

                if(radioButton4x6.isChecked( ))   {
                    if (inputUser <50) {
                        Number = small * inputUser;
                        Result.setText(tenth.format(Result) + " is your total cost!");

                    } else {
                        Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();

                    }
                }
                if (radioButton5x7.isChecked( ))  {
                    if (inputUser <50) {
                        Number = medium * inputUser;
                        Result.setText(tenth.format(Result) + " is your total cost!");
                    }  else  {
                        Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
                    }
                }
                if (radioButton8x10.isChecked()) {
                    if (inputUser <50) {
                        Number = large * inputUser;
                        Result.setText(tenth.format(Result) + " is your total cost!");

                    }  else {
                        Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

    }
}

Thank you in advance for any assistance that can be provided.

Upvotes: 0

Views: 93

Answers (2)

Morton
Morton

Reputation: 5760

I think that you should change the variable like this:

double inputUser;

and

final EditText inputUser = (EditText) findViewById(R.id.editTextNumber);

inputUser will get coufused about this code

inputUser=Double.parseDouble(Number.getText( ).toString(  ));

If i were you that i will change like this:

final EditText editUser= (EditText) findViewById(R.id.editTextNumber);

editUser=Double.parseDouble(editUser.getText( ).toString(  ));

then like the code editUser <50 it will get the value

Hope it helps.

According to your double prints, change like this:

prints=Double.parseDouble(editUser.getText( ).toString(  ));

then you can use if like this:

if (prints<50)

Upvotes: 1

Vadim
Vadim

Reputation: 4120

To be more precise:

  1. Cannot resolve method'getText()'

mistake is because you called your double as Number after all there is a class java.lang.Number as well. So rename it to number

  1. Double.parseDouble(Number.getText( ).toString( ));

Please, read carefully what do you try to do here - get double as String and parse it back to double... what is the reason to do that?

  1. Operator '<' cannot be applied to 'android.widget.editText','int'

    Operator "*" cannot be applied to 'double', 'andriod.widget.EditText'

You defined a variable inputUser as an object of class EditText and then you try to apply primitive math operators < and * to it.

Fortunately Java is strictly typed language and cannot do that. I'm not familiar with android packages. but perhaps EditText has something like getText() which you can parse to double and then compare or multiply...

Upvotes: 0

Related Questions