java - Why does the program fail to check if an element already exists in an array? -


well, have grades database including student id registration, i'm stuck since have code loops , arrays. know easy me use classes teacher wants loops :/

where i'm stuck @ first case on switch,i have ask student id , validate if it's in array belongs id, tried using 2 do while loops , worked while if it's in array id put in array anyway. tried asking teacher said have seen loops in class , should able without help. i'm pretty sure saw basic examples of loops, not kind of thing have compare 2 arrays. appreciate help, , sorry bad english. reading post.

public class reg_al {     public static void main(string[] args) {         scanner sc = new scanner(system.in);         string evaluation[] = {"id", "group", "exam 1", "exam 2", "exam 3"};         int data[][] = new int[10][6];         int opc, x, = 0;         boolean b = true, = false;         {             system.out.println("uanl-fcfm");             system.out.println("menu");             system.out.println("1) student registration");             system.out.println("2) grades");             system.out.println("3) calculate final grade");             system.out.println("4) grades report");             system.out.println("5) final grade report");             system.out.println("6) exit");             system.out.println("choose desired option:");             opc = sc.nextint();             switch (opc) {                 case 1:                     {                         {                             system.out.println("introduce id");                             x = sc.nextint();                             (int j = 0; j < 10; j++) {                                 if (x == (data[j][0])) {                                     system.out.println("id exists");                                     = true;                                 }                             }                         } while (a);                         data[i][0] = x;                         i++;                     } while (b);                     break;                 case 2:                     break;                 case 3:                     break;                 case 4:                     break;                 case 5:                     break;                 case 6:                     system.out.println("thank using student database");                     break;             }         } while (opc != 6);     } } 

your problem seems arise because in end of inner do-while loop in first case of switch construct, add value of x array anyway. also, outer do-while loop seems become infinite control variable b has value true , never changed. means value of x gets added indexes of array. further, throws arrayindexoutofbounds exception when i's value exceeds 9. program overly complicated. suggest use simple logic work done easily:

switch(opc){     case 1:      if(i==9){         system.out.println("array full");     }     else{         system.out.println("introduce id");         x=sc.nextint();         boolean has=false;         for(int j=0; j<10; j++){             if(data[j][0]==x){                 has=true;                 break;             }         }         if(has)         system.out.println("id exists");         else         data[i++][0]=x; //add value of x here     }//outer else      break; //case 1 break     ...     ... }//switch 

note added check value of i in beginning of case. that's prevent arrayindexoutofbounds exception when array full. tested code , worked fine. hope solves problem.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -