0

In my program this is one switch case but it is not executed can u give me the solution

                    Resources r2 = getResources();
                String[] strtemp409A=r2.getStringArray(R.array.strtemp409A);
                String[] strtemp11=r2.getStringArray(R.array.strtemp11);
                String[] strtemp113=r2.getStringArray(R.array.strtemp113);
                String[] strtemp114=r2.getStringArray(R.array.strtemp114);
                String[] strtemp12=r2.getStringArray(R.array.strtemp12);
                String[] strtemp123=r2.getStringArray(R.array.strtemp123);
                String[] strtemp124=r2.getStringArray(R.array.strtemp124);
                String[] strtemp13=r2.getStringArray(R.array.strtemp13);
                String[] strtemp134A=r2.getStringArray(R.array.strtemp134A);
                String[] strtemp22=r2.getStringArray(R.array.strtemp22);
                String[] strtemp23=r2.getStringArray(R.array.strtemp23);
                String[]strtemp401A=r2.getStringArray(R.array.strtemp401A);
                String[] strtemp401B=r2.getStringArray(R.array.strtemp401B);
                String[] strtemp402A=r2.getStringArray(R.array.strtemp402A);
                String[] strtemp402B=r2.getStringArray(R.array.strtemp402B);
                String[] strtemp403B=r2.getStringArray(R.array.strtemp403B);
                String[] strtemp404A=r2.getStringArray(R.array.strtemp404A);
                String[] strtemp407C=r2.getStringArray(R.array.strtemp407C);
                String[] strtemp408A=r2.getStringArray(R.array.strtemp408A);

                String[] strtemp410A=r2.getStringArray(R.array.strtemp410A);
                String[] strtemp414B=r2.getStringArray(R.array.strtemp414B);
                String[] strtemp416A=r2.getStringArray(R.array.strtemp416A);
                String[] strtemp417A=r2.getStringArray(R.array.strtemp417A);
                String[] strtemp500=r2.getStringArray(R.array.strtemp500);
                String[] strtemp502=r2.getStringArray(R.array.strtemp502);
                String[] strtemp503=r2.getStringArray(R.array.strtemp503);
                String[] strtemp507=r2.getStringArray(R.array.strtemp507);
                String[] strtemp508B=r2.getStringArray(R.array.strtemp508B);
                String[] strtempc409A=r2.getStringArray(R.array.strtempc409A);
                String[] strtempc410A=r2.getStringArray(R.array.strtempc410A);
                String[] strtempc414B=r2.getStringArray(R.array.strtempc414B);


    public void setRefno(int refno)
    {

        this.refno=refno;
    }
    public int getRefno()
    {
        return refno;
    }
    public void setStr(String str)
    {
        this.str=str;


    }
    public String getStr()
    {

        return str;
    }
    public double getPrs()
    {
        switch(refno)
         {
             case 0:

                 for (int i = 0; i < strtemp409A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp409A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 1:
                 for (int i = 0; i < strtemp414B.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp414B[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 2:
                 for (int i = 0; i < strtemp416A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp416A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 3:
                 for (int i = 0; i < strtemp404A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp404A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 4:
                 for (int i = 0; i < strtemp507.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp507[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 5:
                 for (int i = 0; i < strtemp502.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp502[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 6:
                 for (int i = 0; i < strtemp402A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp402A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 7:
                 for (int i = 0; i < strtemp402B.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp402B[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 8:
                 for (int i = 0; i < strtemp408A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp408A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 9:
                 for (int i = 0; i < strtemp11.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp11[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 10:
                 for (int i = 0; i < strtemp113.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp113[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 11:
                 for (int i = 0; i < strtemp114.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp114[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 12:
                 for (int i = 0; i <strtemp123.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp123[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 13:
                 for (int i = 0; i < strtemp124.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp124[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 14:
                 for (int i = 0; i < strtemp500.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp500[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 15:
                 for (int i = 0; i < strtemp13.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp13[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 16:
                 for (int i = 0; i < strtemp23.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp23[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 17:
                 for (int i = 0; i <strtemp503.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp503[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 18:
                 for (int i = 0; i < strtemp508B.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp508B[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 19:
                 for (int i = 0; i < strtemp403B.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp403B[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 20:
                 for (int i = 0; i <strtemp22.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp22[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 21:
                 for (int i = 0; i < strtemp407C.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp407C[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 22:
                 for (int i = 0; i < strtemp417A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp417A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 23:
                 for (int i = 0; i < strtemp410A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp410A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 24:
                 for (int i = 0; i < strtemp12.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp12[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 25:
                 for (int i = 0; i < strtemp134A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp134A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 26:
                 for (int i = 0; i <strtemp401A.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp401A[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
             case 27:
                 for (int i = 0; i < strtemp401B.length; i++)
                 {
                     if (str.equalsIgnoreCase(strtemp401B[i]))
                     {
                         prs = i;
                     }
                 }
                 break;
         }

         return prs;





    }
3
  • 3
    the goggles, they do nothing! Please, do yourself a favor and go read up on Refactoring xradiograph.com/Programming/Refactoring Commented Sep 16, 2010 at 11:30
  • You should take that thing out back and shoot it. Commented Sep 16, 2010 at 12:54
  • 1
    I posted my comment too early to be polite; why did I answer your question with a sideways reference? Because there is too much code to understand; refactor it -- slim it down -- this requires understanding how it works, and if THAT doesn't help you find the problem, it will still make it that much easier to find the root problem. Commented Sep 16, 2010 at 13:32

4 Answers 4

2

You haven't told us what refno is. Is that really all one switch case? You can set breakpoints in your code and debug it (F11 in Eclipse) to be able to step through your code and see exactly which execution path it takes.

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

1 Comment

i got that from context, yeah. you really need to be more specific, but regardless, following my example should get you all the information you need as to why your code isn't being executed
1
  1. For what value of refno is it not executed? (I see there is no default case in your switch)

  2. Don't Repeat Yourself... all that repetitive code! You will gain a lot of time if you create a "find_and_print" function, an a dispatch table that calls that function with the proper arguments.

Here is some pseudo-code.

void find_in_array_and_print( String[] strings, String value ) {
   for (int i = 0; i < strings.length; i++) {
     if (strings[i]==value ) {
        prs = i;
        System.out.println("prs"+prs);
        System.out.println("i"+i);
     }
   }    
}

String[] to_look_in( int refno ) {
   switch( refno ) {
     case 0: return R.array.strtemp409A;
     case 1: return R.array.strtemp414B;
     //...
   }
}

find_and_print( GetResources().getStringArray( to_look_in( refno ) ), str );

Comments

0

For that piece of code, the only way you don't get in the switch is if an exception is thrown between the first line and the switch. If you are sure that you even enter that piece of code.

A possible cause of exceptions? Out of memory, null pointer dereference are the first ones that come to mind regarding the limited set of statements.

1 Comment

"In my program this is one switch case" <- unless all of it really is one switch case, which is not being hit.
0

I don't know how you are setting refno, but the auto generated R.java won't start at 0. If you want to trap it add a default: case to your switch. Finally "==" is NOT the way to compare Strings in Java. It won't always work. Try running this as an example and see what happens!

    String s1 = new String("123");

    String s2 = new String("123");
    if (s1 == s2) 
        System.out.println("s1 == s2 SHOWS TRUE");
    else
        System.out.println("s1 == s2 SHOWS FALSE");
    if (s1.equals(s2)) 
        System.out.println("s1.equals(s2) SHOWS TRUE");
    else
        System.out.println("s1.equals(s2) SHOWS FALSE");

Then Google for WHY it doesn't work and why you should use .equals()

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.