0

Our teacher taught us about if-else statement and he's teaching us about how switch statement can be an alternative with if-else so I was coding and I get this unreachable statement error. I donno how I'm suppose to debug coz I just updated my Java from 6 to 8, and I don't really know what's wrong with my code.

import java.io.*;
import java.util.Scanner;
class Switch
{
    public static void main(String[] args)
    {
        Scanner k = new Scanner(System.in);

        System.out.println("Whats your name?");
        String name = k.nextLine();

        System.out.println("Do you want to compute your grades?");
        String yesNo = k.nextLine();

        switch(yesNo)
        {
            case "yes":
                System.out.println("Input your attendance grade(10%)\n");
                int attendance = k.nextInt();
                System.out.println("Input your participation grade (10%)\n");
                int participation = k.nextInt();

                System.out.println("Input your quiz grade (10%)\n");
                int quiz = k.nextInt();

                System.out.println("Input your exam grade(40%)\n");
                int exam = k.nextInt();

                System.out.println("Input your lab grade (20%)\n");
                int lab = k.nextInt();

                double point10 = 0.10;
                double point20 = 0.20;
                double point40 = 0.40;

                double attendanceC = attendance*point10;
                double participationC = participation*point10;
                double quizC = quiz*point20;
                double examC = exam*point40;
                double labC = lab*point20;

                double yourGradeD = attendanceC+participationC+quizC+examC+labC;
                int yourGrade = (int)yourGradeD;
                break;

            default:
                System.out.println("Thank you "+name);
                break;

                switch(yourGrade)
                {
                    case 99|98|97|96|95|94|93|92|91|90:
                        System.out.println("testing");
                        break;
                }

        }

    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 9
    `case 99|98|97|96|95|94|93|92|91|90:` doesn't do what you think it does. – Pshemo Oct 04 '15 at 12:11
  • 4
    The error is caused by a `switch(yourGrade)` after a `break` within `default:` clause. Note that the `switch(yourGrade)` could have been other any Java statement, and the error would still occur. The reason is that the code can never be reached, as you've just "jumped out" of the `default:` clause via `break`. – Alex Shesterov Oct 04 '15 at 12:13
  • 1
    if (yourGrade >=90 && yourGrade <=99) {sysout (testing);} – QuakeCore Oct 04 '15 at 12:16
  • Also note that the problem is _not_ Java8-specific; the only Java8 feature I see is a switch on String (a Java7 feature, to be precise). – Alex Shesterov Oct 04 '15 at 12:16
  • Now that I've fixed your bad indentation, it's more obvious that your `switch(yourGrade)` statement is in the wrong location, don't you think? – Andreas Oct 04 '15 at 12:29
  • where is it suppose to be? i tried doing it outside the switch block but it cant recognized the variable inside. – mark rosales Oct 04 '15 at 12:48

1 Answers1

0

Here is the correct code, your inside switch was indeed unreachable because it was not contained inside of a case.

Here, I put it into your "yes" case which is the most logical.

Note : Your main switch has only two options : "Yes" or "Default". In this case, I would prefer using a simple if...else

  switch(yesNo)
    {
        case "yes":
        System.out.println("Input your attendance grade(10%)\n");
        int attendance = k.nextInt();
        System.out.println("Input your participation grade (10%)\n");
        int participation = k.nextInt();

        System.out.println("Input your quiz grade (10%)\n");
        int quiz = k.nextInt();

        System.out.println("Input your exam grade(40%)\n");
        int exam = k.nextInt();

        System.out.println("Input your lab grade (20%)\n");
        int lab = k.nextInt();

        double point10 = 0.10;
        double point20 = 0.20;
        double point40 = 0.40;

        double attendanceC = attendance*point10;
        double participationC = participation*point10;
        double quizC = quiz*point20;
        double examC = exam*point40;
        double labC = lab*point20;

        double yourGradeD = attendanceC+participationC+quizC+examC+labC;
        int yourGrade = (int)yourGradeD;

        if(yourGrade >= 90 && yourGrade <= 99)
        {
            System.out.println("testing");
        }
        break;


        default:
        System.out.println("Thank you "+name);
        break;
    }
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • looks like im gonna need to study more though were really just about to learn this things im my teacher just made us do an exercise out of nowhere then made it our assignment since we dont have enough time (Without realizing the program is not compatible with java6) – mark rosales Oct 04 '15 at 12:36
  • anyways thanks guys im kind of new here so i dont know how to reply and im kind of busy too hehehe maybe i can call it advance studying – mark rosales Oct 04 '15 at 12:37
  • one more thing is it even possible using switch statement instead of if else statement when what i meant is like im gonna use if else coz i need to use comparison for example if(yourGrade > 100) //codes then do it in switch? – mark rosales Oct 04 '15 at 12:42
  • Yes of course, you can use if inside of a switch or a switch inside of an if.. It doesn't make a problem. @markrosales Do not forget to upvote + accept an answer :) – Yassin Hajaj Oct 04 '15 at 13:01
  • don't use "break" after default. – Irfan Babar Oct 04 '15 at 12:19
  • Its not a general rule, but in the code above the break inhibits the following switch to be executed. – CoronA Oct 04 '15 at 12:42
  • Its not a good way to put break after default, it s the burden on your IDE to compile this extra statement, and more over after the break in above code will never let us to go below . – Irfan Babar Oct 04 '15 at 12:48
  • What if the default is in the middle of other cases? – Yassin Hajaj Oct 04 '15 at 12:59
  • how you can put a default in middle? – Irfan Babar Oct 04 '15 at 13:01
  • Yes, we definitely can. But such code will be ugly and confusing. So we prefer to put default always at last. – Hardik Modha Oct 04 '15 at 13:06
  • @IrfanBabar you are just ignoring the correct practice because of what you think. Let the compiler do the optimization. – Sufian Oct 04 '15 at 15:51
  • @IrfanBabar using the same reason, will you create a tightly coupled program just because it *will* run low on resources? – Sufian Oct 04 '15 at 15:53