-1

I am trying to create a type of triangle app for my homework but when I tried to use if statement in java, for some reason, when I click on the "Generate Button", it keeps on showing the " Scalene Triangle: No Congruent Sides" , even thought my inputs are : 2,2,3 which should be an Isosceles Triangle. Please help. Is my logic wrong or something? Thanks a lot.

Java code: package com.example.trianglegame;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TriangleGame extends Activity {// implement-have to use all of the
                                        // methods
// set up the variables here

Button Gen;
EditText Input1;
EditText Input2;
EditText Input3;
TextView Output1;
TextView Output2;
TextView Output3;
TextView Display;

Editable a;
Editable b;
Editable c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Input1 = (EditText) findViewById(R.id.editText1);
    a = Input1.getText();

    Input2 = (EditText) findViewById(R.id.editText2);
    b = Input2.getText();

    Input3 = (EditText) findViewById(R.id.editText3);
    c = Input3.getText();

    Display = (TextView) findViewById(R.id.textView5);

    // display edit text

    Gen = (Button) findViewById(R.id.button1);

    Gen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if ((a == b && b != c) || (a == c && b != c)
                    || (b == c && a != c)) {
                Display.setText("Isosceles Triangle: 2 Congruent Sides");
            } else if (a == b && a == c && b == c) {
                Display.setText("Equilateral Triangle:All sides are equal");
            }

            else if (a != b && a != c && b != c) {
                Display.setText("Scalene Triangle: No Congruent Sides");
            } else {
                Display.setText("Error");
            }

        }
    });

 }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/enter_text" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/side_1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/side_2" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/type_hint"
    android:text="@string/side_3" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/generate" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>
Chidori
  • 15
  • 3
  • The `==` operator is transitive, you don't need to: `a == b && a == c && b == c` - it's sufficient to perform 2 comparisons: `a == b && a == c`. And as for your problem: print the values of a,b,c to logcat and see if what you have is what you expected to be there. – Nir Alfasi Sep 21 '14 at 07:36
  • hi, I don't see any error on the logcat, must be a human error :( – Chidori Sep 21 '14 at 07:55
  • Your last sentence doesn't make sense: if you print a,b,c and you see their values, you shouldn't be surprised as for which "if" case is entered. How can something like this be a human-error ??? – Nir Alfasi Sep 21 '14 at 08:01

2 Answers2

1

click on the "Generate Button", it keeps on showing the " Scalene Triangle: No Congruent Sides"

Because you are getting all EditText Values before onClick of "Generate Button".

To fix issue call EditText.getText() from onClick of Button

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

You are comparing String references, not numbers. You should use Integer.parseInt (or Float.parseFloat if you allow float values) to convert the text entered by the user before doing any comparisons.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
  • hi, I tried changing everyone's input, but it's still giving me the same results, now it only says " all sides are equal" . Here is my new code. Input1 = (EditText) findViewById(R.id.editText1); int a=Integer.parseInt(Input1.getText().toString()); Input2 = (EditText) findViewById(R.id.editText2); int b=Integer.parseInt(Input1.getText().toString()); Input3 = (EditText) findViewById(R.id.editText3); int c=Integer.parseInt(Input1.getText().toString()); Display = (TextView) findViewById(R.id.textView5); – Chidori Sep 21 '14 at 07:52