user2296226
user2296226

Reputation: 75

Why do you have to declare "Integer int1" before I can use it in an onCreate and in a onClick method?

There was another answer that was related to this, but I didn't understand it.. I'm very new to everything related to coding, so go easy on me please. What I have:

package com.example.robert.rekenmachine;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.view.View;
import android.widget.TextView;





public class MainActivity extends ActionBarActivity {

    EditText num1text, num2text;
    TextView ans;




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

        num1text = (EditText)findViewById(R.id.num1text);
        num2text = (EditText)findViewById(R.id.num2text);
        ans = (TextView)findViewById(R.id.ans);
        Integer int1 = Integer.parseInt(num1text.getText().toString()), int2 = Integer.parseInt(num2text.getText().toString());
        Float ft1 = Float.parseFloat(num1text.getText().toString()), ft2 = Float.parseFloat(num2text.getText().toString());


    }

    public void add(View v){
        Float ansft = ft1

        ans.setText(Integer.toString(1));


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

So at the Float ansft = ft1, it says that it can't resolve the symbol. I was wondering, why? So what I tried was to do the same things I did with num1text, num2text and ans, so I typed: Float ft1;. Now it suddenly could resolve the symbol. I was just wondering if someone could explain it, since I think it would help a great deal in the future if I understand the logic behind it all.

Upvotes: 0

Views: 282

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109577

ans is declared in the scope { ... } of the class, and accessible by all methods as field of the object.

int1 is a local variable in one method. The method's scope is restricted to its braces { ... } and the variable will live during the call to the method. It is not accessible outside.

Now one could make int1 a field. But you probably want to fetch the most recent value of the text field num1text.

private int getInt1() {
    int int1 = Integer.parseInt(num1text.getText().toString());
    return int1;
}

Or simply:

private int getInt1() {
    return Integer.parseInt(num1text.getText().toString());
}

And do

public void add(View v) {
    int int1 = getInt1(); // Take latest value.
    ans.setText(Integer.toString(int1));

Or shorter

    ans.setText(Integer.toString(getInt1()));
}

Upvotes: 1

JFPicard
JFPicard

Reputation: 5168

ft1 is not defined in your method public void add(View v){ to make it accessible, try to change the method to public void add(View v, Float ft1){

You can also put Float ft1; as an attribute.

Check about java scope to get more details on it.

Upvotes: 0

Related Questions