java - problem in execution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reva
    New Member
    • Apr 2007
    • 7

    #1

    java - problem in execution

    hi all ,
    I have used switch case to check whether a particular character is present in another array of characters. i want the code to do if the particular character is found then paint function is called and the character is given a colour. but what happens is that the if loop in paint method is not getting executed. also the else codition in the paint method paints for the number of characters present in the array. (but the check is proper (i suppose) based on the print statement in the switch case)

    here is my code:
    Code:
    public void assigning()// function
        {   
            for (int i=0;i<seqlength;i++)//a for loop to get a letter 
            {   
                switch(seq[i])// main array to which the cases are compared
                {
                    case 'A':
                    case 'G':
                    case 'I':
                    case 'L':
                    case 'M':
                    case 'F':
                    case 'P':
                    case 'W':
                    case 'V':
                    {
                        res=1;
                        repaint();
                        System.out.println("reach");
                        break;
                    }
                    case 'R':
                    case 'N':
                    case 'D':
                    case 'C':
                    case 'E':
                    case 'H':
                    case 'K':
                    case 'S':
                    case 'T':
                    case 'Y':
                    {
                        res=0;
                        repaint();
                        System.out.println("reach");
                        break;
                        
                    }
                    default:
                        
                        repaint();
                        
                }
            //return res;
        }
      }
            
        public void paint(Graphics gc) 
        
        { 
            String s = "reach";
            gc.drawOval(centre1, centre2, 2* radius, 2*radius);
            gc.setFont(new Font("Courier",Font.BOLD,14));
            gc.drawString(title,700,380);
            gc.drawString(sequen,700,400);
            gc.drawString(sequences,900,400);
            gc.drawString(hydropho,700, 420);
            gc.drawString(hydrophl, 700,440);  
            if(res==1)
            {
                gc.drawString(s, 700,700) ;// not executing the loop
                 for (int i =0;i<seqlength;i++)
             {
              
            for (int j=0; j<hydroblen;j++)
            {       
                x[i]= x0 + radius*Math.sin(theta*(i));
                y[i]= y0 - radius*Math.cos(theta*(i));
                X[i] = (int) x[i];
                Y[i]= (int) y[i];
                gc.fillOval(X[j]-7,Y[j]-7,14,14);
                seq1 = seq[i] + "";
                //hydrop1 = hydrop[j]+"";
                gc.drawString(seq1, X[i], Y[i]-8); 
                gc.drawString("",X[j], Y[j]-8);
                gc.drawLine(X[j],Y[j],X[j+1],Y[j+1]);
                gc.setColor(Color.yellow);
                
            }
            }
            }
            else if (res ==0)
            {
                {   // executes here.
                for (int i=0; i<seqlength;i++)
                {
                    for (int j= 0; j<hydroplen;j++)
                    {
                    gc.drawString(s,250,250);//here too
                    x[i]= x0 + radius*Math.sin(theta*(i));
                    y[i]= y0 - radius*Math.cos(theta*(i));
                    X[i] = (int) x[i];
                    Y[i]= (int) y[i];
                    gc.fillOval(X[j]-7,Y[j]-7,14,14);// results in filling the oval equal to the hydroplen and not accor to charac.
                    seq1 = seq[i] + "";
                    //hydrop1 =hydrop[j]+"";
                    gc.drawString(seq1,X[i],Y[i]-8);
                    gc.drawString("", X[j], Y[j]-8);
                    gc.drawLine(X[j],Y[j],X[j+1],Y[j+1]);
                    gc.setColor(Color.blue);
                    
                    }
                }
            }
            
            }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What is res? Is it an instance variable? In your assigning() function, you have return res;...but have commented it out. Also, I think there is a logic error in this function. You are assigning 1 or 0 to res, but this check is done for each character in the array. Thus, at the end of the loop, the value in res only reflects the result of the last character, not all the characters. If you return res inside the loop, then the loop is useless - it will only execute once! This looks like it needs some re-thinking.

    Comment

    • reva
      New Member
      • Apr 2007
      • 7

      #3
      Originally posted by Ganon11
      What is res? Is it an instance variable? In your assigning() function, you have return res;...but have commented it out. Also, I think there is a logic error in this function. You are assigning 1 or 0 to res, but this check is done for each character in the array. Thus, at the end of the loop, the value in res only reflects the result of the last character, not all the characters. If you return res inside the loop, then the loop is useless - it will only execute once! This looks like it needs some re-thinking.

      hi ,
      thanks , yes res is an instance variable. since assigning is a void method it cant return residue. so i have assigned a value to that. so how could i get the
      value for all the character. is there any other method that could be used.

      but as u ve said if it will return only result of the last character then i shuld have to get that a single residue to be painted. but i am getting 10 residues (lenght of the hydrop) to be painted and not according to the colour.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You will have to check each character in seq individually and print/change/do whatever based on that result. Alternatively, you can check all the characters in seq in the function as you are now, but you should then have res be an array, with the ith index of res corresponding to the ith character in seq. Does that make sense?

        Comment

        • reva
          New Member
          • Apr 2007
          • 7

          #5
          Originally posted by Ganon11
          You will have to check each character in seq individually and print/change/do whatever based on that result. Alternatively, you can check all the characters in seq in the function as you are now, but you should then have res be an array, with the ith index of res corresponding to the ith character in seq. Does that make sense?

          hi ,
          thanks have got it....

          Comment

          Working...