0

I have an “EditText” called Inputs1 to enter values to compare with the Array example1 using an “If” statement.

The Array named example1 has these values: 5 , 10, 15, 20, 25, 30

Outputs1 is used to display values from the Array example1

Also, an Array named example2 have these values: 105, 110, 115, 120, 125, 130

Outputs2 is used to display values from the Array example2 using the index value from the Array example1

Example:

The Inputs1 value is: 20

Outputs1 displays the value: 20

Outputs2 should display the value: 120

I went as far as making Outputs1 to display the value: 20, but I cannot get the code for Outputs2 to display the value: 120.

Please, anyone with the above experience or knowledge help me?

Thanks.

Code:

    public class exampleOutput extends AppCompatActivity {

    Editable text;
    EditText Inputs1;
    TextView Outputs1;
    TextView Outputs2;
    Integer arrayIndex;


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

        Outputs1 = (TextView) findViewById(R.id.Outputs1);
        Inputs1 = (EditText) findViewById(R.id.Inputs1);
        text = Inputs1.getText();
        Outputs2 = (TextView) findViewById(R.id.Outputs2);



    }

    final String[] example1 = {("5"), ("10"), ("15"), ("20"), ("25"), ("30")};
    final String[] example2 = {("105"), ("110"), ("115"), ("120"), ("125"), ("130")};


    public void displayExample(View v) {

        if (Arrays.asList(example1).contains(Inputs1.getText().toString())) {

            Outputs1.setText("You typed in: " + text + ", which is correct!");
            arrayIndex = (Arrays.asList(example1).indexOf(Inputs1.getText().toString()));
            Outputs2.setText("Your second number is: [The value from the second array] " +  ", the index is: " + arrayIndex + "!");

            } else {

            Outputs1.setText("Please type in a number 1-5.");
            Outputs2.setText("");

      }

   }

}
2
  • Please post your code on how you get value for Label1 Commented Dec 3, 2018 at 2:15
  • what's your current code? Commented Dec 3, 2018 at 2:15

2 Answers 2

1

you can use

int indexFromLabel1 = array1.indexOf(valueWhoseIndexYouNeed);

Then, if I unserstand correctly, you want to show the value at this index in Array2. This you must already have figured out

int valueFromArray2 = array2[indexFromLabel1 ];

This is assuming I understood the scenario correctly.

Better yet, you should apply check to see if the required index exists in Array2, like so

if( indexFromLabel1 < array2.length() ){
    int valueFromArray2 = array2[indexFromLabel1 ];
}else {
    //Do your logic

}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked because of the array2[indexFromLabel1 ], thanks for helping me on that, I was trying to figure out how to print the value.
glad to hear that.
1

Looks like you never print the value from example2 array

Try changing the this line:

Outputs2.setText("Your second number is: [The value from the second array] " +  ", the index is: " + arrayIndex + "!");

to this:

Outputs2.setText("Your second number is: [The value from the second array] " + example2[arrayIndex] + ", the index is: " + arrayIndex + "!");

1 Comment

I tried to print the value but I never knew how, now it works because of the + example2[arrayIndex], thanks for helping me on that. Sadly, the other person responded first, so I can't put this as a correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.