arrays - Stuck in Vector in Java -


i created string vector , want store selected type of coffee in vector, example if choose number 3,4,5 , 1, store them in vector1 variables ("latte", "american", "capuccino" , "moka") , print stored names, problem prints, number 6 "black", always.

 import java.io.*;      class exercise{          public static void main (string[] args) throws java.io.ioexception {             datainputstream receivedata= new datainputstream(system.in);             string keyboard;                  int seleccafe,n=0,contvector=0;                 string []vector1=new string[200];                              do{                                   system.out.println("\n1. moka");                                  system.out.println("2. expreso");                                  system.out.println("3. latte");                                  system.out.println("4. american");                                  system.out.println("5. capuccino");                                  system.out.println("6. black");                                  system.out.println("0. out");                                  system.out.print("\ntype number: ");                                   seleccafe=read_da("");                                  selectco(vector1,seleccafe,n);                                   contvector++;                                  n++;                                  if(seleccafe==0) break;                              }while(true);                                  for(int i=0; i<contvector-1; i++){                                 system.out.println(vector1[i]);                                 }          }             public static int  read_da (string txtmsg) throws java.io.ioexception{                 int x=0;                 string keyboard;                 datainputstream reveivedata= new datainputstream(system.in);                 do{                     system.out.print("");                     keyboard=reveivedata.readline();                     try{                         x=integer.parseint(keyboard);                         if(x<0){                             system.out.println("select 0+");                             continue;                         }                         return x;                      }catch(numberformatexception e){                         system.out.println("just numbers");                              }                 }while(true);         }          public static void selectco(string xvector[], int xseleccafe, int xi){              switch(xseleccafe){                 case 1:                     xvector[xi]= "moka";                 case 2:                      xvector[xi]="expresso";                 case 3:                      xvector[xi]="late";                 case 4:                      xvector[xi]="american ";                 case 5:                     xvector[xi]="capuccino";                  case 6:                      xvector[xi]="black";             }         }      } 

any help? thanks!

your problem missing word break; after each case clause in selectco, each case "falling through" 1 below, , finishing case 6:.

it should this.

      switch(xseleccafe){             case 1:                 xvector[xi]= "moka";                 break;             case 2:                  xvector[xi]="expresso";                 break;             case 3:                  xvector[xi]="late";                 break;             case 4:                  xvector[xi]="american ";                 break;             case 5:                 xvector[xi]="capuccino";                  break;             case 6:                  xvector[xi]="black";                 break;             default:                 xvector[xi]="unknown";         } 

more detail here


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -