2016-06-11 1 views
0

안녕 얘들 아 내 문제를 도와 줄 수있어. 난 내 세트에 번호를 할당 몇 개의 프로그램이 제대로 실행 할 수 있으며 일부 경우스레드 "main"의 예외 java.lang.ArrayIndexOutOfBoundsException : 3 내 predermined 세트에서 no로 변경하면 오류가 발생합니다.

이 문제가 더 3 MainApp.main에서 (MainApp.java:43) : 내가 스레드에서 "주요"java.lang.ArrayIndexOutOfBoundsException을 예외있어 위의 오류를 줄 것이다. 1 - 난 당신의 코드에서 본 것을에서

import java.util.*; 

public class MainApp { 

    public static void main (String [] args){ 

     //System.out.println("Enter your number?"); 

     //Scanner scn = new Scanner(System.in); 

     //declare array 
     String set[] = new String[3]; 


     set[0] = "2,9"; // change the number in this set some number give error while some do not have error 
     set[1] = "3,7"; 
     set[2] = "8,2"; 


     //store number in array set 
//  for(int i=0;i < 3;i++){ 
//   System.out.println("Input for " + i); 
//   set[i] = scn.nextLine(); 
//   
//  } 

     //determine whether it is transitive or not 
     int a = 0; 
     int d = 0; 
     int b = 0 ; 

     for(int i =0 ;i<3 || d == -1 ;i++) 
     { 

      for(int j= 0 ;j<3 || d == -1 ;j++) 
      { 
       a= 0 ; 

       if (i == j) 
        a = 2 ; 

       else if(set[i].charAt(2) == set[j].charAt(0)) //this line that returned error 
        for(int k =0 ;k<3 ;k++) 
        { 
         if (set[k].charAt(0)==set[i].charAt(0) && set[k].charAt(2)==set[j].charAt(2)) 
         { 
          a= 1; 
          b++ ; 
         } 

        } 
       else 
        a = 2 ; 

       if(a== 0) 
        d= -1 ; 


      } 


     } 

     if(b == 0 || d == -1) 
      System.out.println("Invalid"); 
     else 
      System.out.println("Valid"); 

      System.out.println("***********************************************************"); 
      //reflexive 
      //cnt used to determine whether all the no have their reflexive 
      //variable reflex is used to store value of set so that we can compare with set 
      //to determine whether the no in set got reflexive or not 
      //variable g is used to determine whether the no have reflexive or not 

      int cnt = 0 ; 
      int g = 0 ; 
      char reflex[] = new char[3*2]; 
      int t = 0 ; 

      //insert set value to reflex variable 
      for(int m= 0; m<3; m++) 
      { 
       reflex[t] = set[m].charAt(0); 
       t++ ; 
       reflex[t] = set[m].charAt(2); 
       t++ ; 
       } 

      //determine whether all of no have reflexive or not 
      for(int n=0 ; n<6 ;n++){ 
       g= 0 ; 
       for(int o=0 ; o<3 ;o++) 
        if(set[o].charAt(o) == reflex[n] && set[o].charAt(2) == reflex[n]) 
         g= 1 ; 
       if(g == 1) 
        cnt++ ; 

      } 

      if(cnt == 6) 
       System.out.println("Valid Bro"); 
      else 
       System.out.println("Invalid"); 



      System.out.println("***********************************************************"); 
      //symmetry 
      int u = 0 ; 
      int count = 0 ; 

      for(int r= 0 ;r<3 ;r++) 
      { 
       u = 0; 
       for(int s =0 ;s<3 ;s++) 
       { 
        if(set[s].charAt(0)== set[r].charAt(2) && set[s].charAt(2)== set[r].charAt(0)) 
         u= 1; 
       } 
       if(u==1) 
        count++ ; 
      } 

      if(count == 3) 
       System.out.println("Valid Bro"); 
      else 
       System.out.println("Invalid"); 

     } 

    } 
+0

해야한다 2, 나중에 코드에서 집합 [3]에 액세스하려고하지만 집합은 길이가 3이므로 0, 1 및 2입니다.이를 수용하기 위해 일부 논리를 변경해야합니다. i 루프와 동일합니다. – Vilsol

+0

중단 점을 지정하고 코드를 따르십시오. –

답변

0

,이 경우 J < 3 || D == 확인하려는 이유를 정말 확실하지 않다 위반 라인

for(int i =0 ;i<3 || d == -1 ;i++) 
      { 
       System.out.println("Outer Loop "+i); 


       for(int j= 0 ;j<3 || d == -1 ;j++) 
       { 

을 것 같다 무슨 일이 일어나는지는 i = 2, j-2 일 때입니다. d의 값은 -1로 설정됩니다. 이것은 내부 루프가 여분의 반복을 취하는 것을 의미하며, 예외가 발생합니다. 당신이 정말로 D를 확인해야하지 않는 한 == - 1 나는 원인이 J GET의 위 증가 할 때 J 루프도 실행할 수 있다는 확신 부분을 제거하고 그것이 마치 마법처럼 작동하므로

for(int i =0 ;i<3 ;i++) 
       { 
        System.out.println("Outer Loop "+i); 


        for(int j= 0 ;j<3 ;j++) 
        { 
관련 문제