1

In my project i have a loop that is a simulation of a choice menu and when the user chooses a number i want to go to the next loop but that is a part of a switch statement that is inside the loop. My code is:

while(entrychoice!=8){     

    String alphabet= "abcdefghijklmnopqrstuvwxyz";  //tyxaio onoma    

       String   onoma = "";

      Random random = new Random();
      int randomLen = 1+random.nextInt(9);
    for(int p = 0; p < randomLen; p++) {
      char c = alphabet.charAt(random.nextInt(26));
       onoma+=c;
            }

            //Random rand=new Random();//this.kratisid=kratisid; kai stin main bazo random
            //int kratisid=rand.nextInt(500)+100;//Παραγει τυχαιο κωδικο κρατησης μεταξυ 100 και 500




 int kratisid = getRandomInt();  //dimiourgia monadikou kai tyxaiou                                    kratisid

    Random rand1=new Random();
    int afixi=rand1.nextInt(30)+1;//tyxaia mera afixis se ena mina

    Random rand2=new Random();
    int mdiam=rand2.nextInt(30)+1;//tyxaies meres diamonis se ena domatio

    Random rand3=new Random();
    int atoma=rand3.nextInt(4)+1;

    Domatio d=null;//reference to the domatio pou egine kratisi


    reservation r=new reservation(onoma,kratisid,afixi,mdiam,atoma,d);
    //r.antistoixisidtodom(d,dom);

   Hotel1.addkratisianyroom(r);//prosthiki  tyxaias kratisis r se tyxaio domatio

   //25% pithanotita gia tyxaia akyrosi kratisis
   Random randprob=new Random();
   int prob=randprob.nextInt(4)+1;//prob is the probability of cancelling a random kratisi
   if (prob==1)//25%
   {  //epilogi tyxaias kratisis apo tin lista kratiseon Rlist gia akyrosi
        Random randomGeneratorList=new Random();
        int index=randomGeneratorList.nextInt(Rlist.size())+0;//+0;

       int kratisiFRCID=Hotel1.Rlist.get(index).kratisid;//kratisiFRCid means kratisiForRandomCancel ID
        Hotel1.cancelkratisi(kratisiFRCID);//Rlist.get(index).cancel....
    }



         Scanner scanchoice = new Scanner(System.in);
         System.out.println();
         System.out.println("Enter \"1\", \"2\",\"3\",\"4\",\"5\",\"6\",\"7\"or\"8\"");
          if(scanchoice.hasNextInt()){
        entrychoice = scanchoice.nextInt();
    }
  switch(entrychoice)
  {
     case 1:
     continue;
     break;**
    }

}

Note that the code continues but is huge and the other options are not the matter.As you see in case1 of the switch statement i want if the user inputs 1 to go to the next loop but the compiler says that break; will never be executed which is something that disrupts my switch statement.Please suggest how to go to next loop with a switch statement and user input as i have done here.Thx in advance.

George J
  • 11
  • 5
  • 1
    If you have another question, ask it as a separate question, with its own title, answers, votes, comments, etc. Nobody will see it here. – user207421 Apr 30 '17 at 11:00
  • @davidxxx I don' know how exactly you expect me to respond to a deleted post, but the first sentence of it is incorrect. `continue` can be used anywhere inside a loop, including `switch` statements. – user207421 Apr 30 '17 at 11:05
  • @ EJP Sorry.I definitively deleted.Indeed you are right. – davidxxx Apr 30 '17 at 11:09

1 Answers1

2

The compiler is right, break is never reached. All you need to do is removing it:

switch(entrychoice) {
    case 1:
        continue;
    case 2:
        ...
        break;
}

The reason for having break is to end the case by sending execution to the end of the switch. Since continue also ends the switch by sending execution to a different point of your program, break becomes unreachable, so Java compiler insists on removing it.

Same goes for other constructs that break execution: you do not need a break after a return or throw inside a switch.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • also sorry for asking again i have break; break; in the last switch statement so as to my programm to exit the genneral while loop if i leave only one break; i exit the loop right? – George J Apr 30 '17 at 10:55
  • @GeorgeJ `break` inside a `switch` breaks the `switch`, not the loop. [Here is Q&A explaining how to exit a loop from inside a `switch`](http://stackoverflow.com/a/22823427/335858). – Sergey Kalinichenko Apr 30 '17 at 11:03