0

i am developing an android app in which 1 edit text is there... the first edittext is to give firstname of the user .. in which letters are entered for example first name of users... and for each letter different values are given ... my question how to add all these values.... ?

HOW TO take each letters individually.....and put into an array and add all its values

    public class MainActivity extends Activity {

    Button btn1;
    EditText et1, et2, et3, et4, et5, et6;
    Spinner sp1,sp2,sp3;
    TextView txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10;
    String month,day,year;
    int sum;

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

    }

    public void calc(View v)
    {

        EditText et1 = (EditText) findViewById (R.id.editText1);
        /*TextView txt1 = (TextView) findViewById ( R.id.textView88);*/
        sum=getSum(et1.getText().toString());
        Intent i=new Intent(MainActivity.this, result.class);
        i.putExtra("name", sum);
        startActivity(i);

        /*txt1.setText(sum + "");*/


    }

    private int getSum(String text) 
    {
        // TODO Auto-generated method stub
        int sum = 0;
        char[] name = new char[text.length()];
        name = text.toCharArray();

        for(int i=0; i<text.length(); i++)
        {
            sum += value( name[i] );
        }
        while (sum>9)
        {
            sum = findDigitSum(sum);
        }

        return sum;
    }



    public int findDigitSum(int n) 
    {
        int sum = 0;
        while (n != 0) 
        {
            sum += n % 10;
            n = n / 10;
        }
        return sum;
    }
    private int value(char a) {
        // TODO Auto-generated method stub
        switch(a) {
        case 'A': 
           return 1;
        case 'B':
           return 2;
        case 'C':
           return 3;
        case 'D': 
            return 4;
         case 'E':
            return 5;
         case 'F':
            return 6; 
         case 'G': 
             return 7;
          case 'H':
             return 8;
          case 'I':
             return 9;
          case 'J': 
              return 1;
          case 'K':
              return 2;
           case 'L':
              return 3;
           case 'M': 
               return 4;
            case 'N':
               return 5;
            case 'O':
               return 6; 
            case 'P': 
                return 7;
             case 'Q':
                return 8;
             case 'R':
                return 9;
             case 'S': 
                 return 1;
             case 'T':
                 return 2;
              case 'U':
                 return 3;
              case 'V': 
                  return 4;
               case 'W':
                  return 5;
               case 'X':
                  return 6; 
               case 'Y': 
                   return 7;
                case 'Z':
                   return 8;
                 default:
            return 0;
    }





}
}

the second activity result.java

public class result extends Activity {

    TextView txt2;
    /*private Intent getIntent;*/



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_xm);
        TextView txt2 = (TextView) findViewById(R.id.textView2);
        txt2.setText(getIntent().getStringExtra("name"));
        /*Intent iin= getIntent();
        Bundle b = iin.getExtras();
        if(b!=null)
        {
            String name = getIntent.getStringExtra("name");
            txt2.setText(name);
        }*/
    }

}

Logcat details

08-26 20:25:47.579: E/Trace(1873): error opening trace file: No such file or directory (2)
08-26 20:25:49.179: D/dalvikvm(1873): GC_CONCURRENT freed 39K, 6% free 2788K/2948K, paused 19ms+89ms, total 195ms
08-26 20:25:50.311: I/Choreographer(1873): Skipped 33 frames!  The application may be doing too much work on its main thread.
08-26 20:25:50.579: D/gralloc_goldfish(1873): Emulator without GPU emulation detected.
08-26 20:27:38.388: D/dalvikvm(1873): GC_CONCURRENT freed 125K, 8% free 3049K/3296K, paused 81ms+21ms, total 257ms
08-26 20:27:40.869: I/Choreographer(1873): Skipped 30 frames!  The application may be doing too much work on its main thread.
08-26 20:27:42.209: D/AndroidRuntime(1873): Shutting down VM
08-26 20:27:42.229: W/dalvikvm(1873): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-26 20:27:42.349: E/AndroidRuntime(1873): FATAL EXCEPTION: main
08-26 20:27:42.349: E/AndroidRuntime(1873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.numerology/com.example.numerology.result}: java.lang.NullPointerException
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.os.Looper.loop(Looper.java:137)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at java.lang.reflect.Method.invoke(Method.java:511)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at dalvik.system.NativeStart.main(Native Method)
08-26 20:27:42.349: E/AndroidRuntime(1873): Caused by: java.lang.NullPointerException
08-26 20:27:42.349: E/AndroidRuntime(1873):     at com.example.numerology.result.onCreate(result.java:19)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.Activity.performCreate(Activity.java:5104)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-26 20:27:42.349: E/AndroidRuntime(1873):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-26 20:27:42.349: E/AndroidRuntime(1873):     ... 11 more
4
  • 1 EditText has 1 value - a String. How / Why do you want to split that into multiple values and put them into an array? Commented Aug 25, 2013 at 19:14
  • i need to find the numerological value of name... for that different letters have different values ... so when user enter his first name all the text have to be taken and the values has to add each other and find the resulted value Commented Aug 25, 2013 at 19:18
  • @ Rachit do u know how to do it Commented Aug 25, 2013 at 19:21
  • stackoverflow.com/questions/10048899/string-to-char-array-java - you can treat a char as a number Commented Aug 25, 2013 at 19:21

2 Answers 2

0

Try this

Write your activity code as:

public class MainActivity extends Activity {

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

public void sum(View v) {
            int sum =0;
    EditText editText = (EditText) findViewById(R.id.editText1);
    sum = getSum(editText.getText().toString());

    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(sum + "");
}
public int getSum(String text) {
    int sum = 0;
    char[] name = new char[text.length()]; // where length is maximum length
                                            // of name.
    name = text.toCharArray();

    for (int i = 0; i < text.length(); i++) {
        sum += value(name[i]);
    }

    // check if sum is greater than 9
    while (sum > 9) {
        sum = findDigitSum(sum);
    }
    return sum;
}

   // find sum of digits if sum is greater than 9

public int findDigitSum(int n) {
    int sum = 0;
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return sum;
}
// return value for char
public int value(char a) {
    switch (a) {
        case 'c' :
            return 7;
        case 'a' :
            return 3;
        case 't' :
            return 9;
        case 'e' :
            return 2;
        default :
            return 0;
    }
}

}

Write your layout file activity_main.cml as:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:ems="10" >

    <requestFocus />
     </EditText>

     <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Sum"
    android:onClick="sum" />

 </RelativeLayout>

Now clicking the button shows you the sum in textView.

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

37 Comments

@ Rachit... i think this will work... thanks a lot... let me check.... and if any doubt I will reply....
oh okay :) ! no probs !
i am not getting the result... i need to display the value in a text view ... how will display it...pls check this is it correct
TextView tv1=(TextView)findViewById(R.id.textView1); tv1.setText(string);
pls check the full code.. i edited it in my question ...as u given
|
0

I think what you want is a Autocomplete Text View. Please go through the link for detailed implementation of Autocomplete Text View.

Comments

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.