-2

Question 1) For some reason, when i click "generate" it does not display any text.I want the button to do the following: after the user input 3 values (int) for example : 1,1,2 and click the "generate" button and the display message should be "Isosceles Triangle: 2 Congruent Sides"---this works before but my logic was wrong and I asked a question here : If statement not working for android programming . then i try to implement the suggestions and now it has a different error

Question 2) Is my logic correct? Will I be able to display the correct result after fixing this problem?

Please help, I am new to android programming. Thanks so much in advance.

java code:

package com.example.trianglegame;

import android.app.Activity;
import android.os.Bundle;
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;
String input1;
String input2;
String input3;

TextView Display;

int a;
int b;
int c;

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

    // assigning the values
    Input1 = (EditText) findViewById(R.id.editText1);
    Input2 = (EditText) findViewById(R.id.editText2);
    Input3 = (EditText) findViewById(R.id.editText3);

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

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

    Gen.setOnClickListener(new View.OnClickListener() {

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

            // getting the text and converting it to string value
            input1 = Input1.getText().toString();
            input2 = Input2.getText().toString();
            input3 = Input2.getText().toString();

            // converting those text values back into int
            a = Integer.parseInt(input1);
            b = Integer.parseInt(input2);
            c = Integer.parseInt(input3);

            // displaying the message

            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) {
                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");
            }

        }
    });
}
}

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" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear" />
</LinearLayout>

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

</LinearLayout>

LogCat Error

09-21 18:50:20.739: E/Zygote(32): setreuid() failed. errno: 2
09-21 18:50:24.939: E/Zygote(32): setreuid() failed. errno: 17
09-21 18:50:25.579: E/BatteryService(58): usbOnlinePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryVoltagePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryTemperaturePath not found
09-21 18:50:25.589: E/SurfaceFlinger(58): Couldn't open /sys/power/wait_for_fb_sleep or  /sys/power/wait_for_fb_wake
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mouse0, Not a typewriter
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mice, Not a typewriter
09-21 18:50:28.379: E/System(58): Failure starting core service
09-21 18:50:28.379: E/System(58): java.lang.SecurityException
09-21 18:50:28.379: E/System(58):   at android.os.BinderProxy.transact(Native Method)
09-21 18:50:28.379: E/System(58):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
09-21 18:50:28.379: E/System(58):   at android.os.ServiceManager.addService(ServiceManager.java:72)
09-21 18:50:28.379: E/System(58):   at com.android.server.ServerThread.run(SystemServer.java:184)
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/Effect_Tick.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressStandard.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressDelete.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressReturn.ogg
09-21 18:50:29.359: E/ThrottleService(58): Could not open GPS configuration file /etc/gps.conf
09-21 18:50:29.819: E/logwrapper(133): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(135): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(136): executing /system/bin/tc failed: No such file or directory
09-21 18:50:32.957: E/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3
Community
  • 1
  • 1
Chidori
  • 15
  • 3
  • what do you want to do on button click..please specify. – Krupal Shah Sep 21 '14 at 18:25
  • 1
    In Java and Android, your object and variable names must begin with lowercase, like `Button gen;`, unless they are `public static final` – joao2fast4u Sep 21 '14 at 18:27
  • hi, i changed the all of the input into lowercase but it's still not displaying the text after i click the generate button – Chidori Sep 21 '14 at 18:34
  • what is your logcat showing???use system.out.println() in the button click to detect wheater the button click is fired – kgandroid Sep 21 '14 at 18:39
  • It's the path to the emulator. I adjusted it and everything works fine out. The main issue was that my button was covering my text, so the code and logic is fine. thanks for helping out. – Chidori Sep 22 '14 at 03:14

2 Answers2

0

Instead of:

input2 = Input2.getText().toString();
input3 = Input2.getText().toString();

use:

input2 = Input2.getText().toString();
input3 = Input3.getText().toString();
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • thanks for noticing that. I tried it but i still get the same error so it must be that and something else. – Chidori Sep 21 '14 at 18:33
  • What is the output of 1,1,2? – Abdallah Alaraby Sep 21 '14 at 18:39
  • At first it wasn't showing anything and it was because my button was covering it :) I know now, everything was fine except for the typo that you showed me. thanks again – Chidori Sep 22 '14 at 03:15
  • good to know, coz i kept staring at your code failing to know what's wrong :D – Abdallah Alaraby Sep 22 '14 at 03:23
  • hahah, I am new to this website so the questions I asked, I think others already asked and for some reason, my reputation points went down... hopefully I can still ask questions in the future. But yeah , i always research the questions and usually can't find an answer that's why I post it :) – Chidori Sep 22 '14 at 03:26
  • you should spend some time reading the [best ways to ask a question](http://stackoverflow.com/help/how-to-ask) and similar topics. If my answer is correct please don't forget to accept and upvote :) – Abdallah Alaraby Sep 22 '14 at 03:32
  • I would love to but I can't upvote :) my reputation is too low, they don't allow me :( – Chidori Sep 22 '14 at 03:51
0

As far as I can see, logic is fine, although I would check in a different way, quite more readable.

int n = 0; //number of equal sides

if(a==b)
  n++;
if(a==c)
  n++;
if(b==c)
  n++;

//Display seems like a class, but is a variable, should start with lower case and have camel notation such as displayTextView
switch(n) {
  case 0:
    Display.setText("Scalen");
    break;
  case 1:
    Display.setText("Isosceles");
    break;
  case 2: case 3:
    Display.setText("Equilateral");
    break;
}

As far as the error it shows, could you post the log?

unmultimedio
  • 1,224
  • 2
  • 13
  • 39
  • hi, when i was running the program, I do not see any error in the LogCat so I cannot post it :( – Chidori Sep 21 '14 at 18:41
  • When the app crashes, if you have your phone connected, LogCat should print something, please try with IDE conencted – unmultimedio Sep 21 '14 at 18:42
  • Hi, thanks for answering. yes, it printed something now for errors. I will edit my post – Chidori Sep 21 '14 at 18:51
  • I think that's not the log we're looking for haha, do you have the exception description? And just to be clear, your xml is game.xml? – unmultimedio Sep 21 '14 at 19:01
  • yeah, it is game.xml . Hi, I did not throw in an exception for the input values. Was I suppose to do that? Do you suggest I look up on how to throw in exception for those input values? – Chidori Sep 21 '14 at 19:05
  • No, exception should be thrown in runtime. Something like NullPointerException or OutOfBoundsIndexException... Idea is to know the line in your code is causing it. – unmultimedio Sep 21 '14 at 19:14
  • LOL, nvm I got it, the buttons were covering the text. Everything works now :) – Chidori Sep 21 '14 at 22:55
  • Haha ok, nice you found it – unmultimedio Sep 22 '14 at 00:13