reckless
reckless

Reputation: 67

Errors with the nested switch cases and string to char

I am experiencing some difficulties trying to code a simple mark range program with my limited amount of skill in the java language. Basically what i am trying to do is to get the user to enter their grading level and i would provide them their mark range. I am experiencing multiple issues with the program at the moment. I am trying to work with nested switch cases, in which i have 0 experience in. I am also required to only use char to get the minus and plus signs, and i have no idea how to add the "char"s of minus and plus sign to the nested switch cases. Any help would be greatly appreciated, thank you all.

Heres my code:

public class SwitchCase {

public static void main(String[] args) {

    String grade;

    String plusorminus ="+-";

    char plus=plusorminus.charAt(0);
    char minus=plusorminus.charAt(1);

     java.util.Scanner input=new java.util.Scanner(System.in);

     grade = input.toString();

    switch(grade)
    {
    case 1:
        switch("A"){
        System.out.println("Your mark range is 85-89.99%");
        case 1: A(0)
        System.out.println("Your mark range is 90-100%");
        case 2: A(1)
        System.out.println("Your mark range is 80-84.99%");
        }

    case 2:
         switch ("B"){
         System.out.println("Your mark range is 73-76.99%");
             case 1: B(0)
             System.out.println("Your mark range is 77-79.99%");
             case 2: B(1)
             System.out.println("Your mark range is 70.72.99%");
         }
     case 3:
         switch("C"){
           System.out.println("Your mark range is 63-66.99%");
           case 1: C(0)
           System.out.println("Your mark range is 67-69.99%");
           case 2: C(1)
           System.out.println("Your mark range is 60 - 62.99%");
         }
     case 4:
         switch("D"){
         System.out.println("Your mark range is 50-54.99%")
           case 1: D(0)
           System.out.println("Your mark range is 55-59.99%");
         }
     case 5:
         switch("F"){
         System.out.println("Your mark range is 0-49.99%")

         }

       }

    }

}

Upvotes: 0

Views: 509

Answers (2)

v4ssili
v4ssili

Reputation: 83

The most simple approach i can think of - without the use of any switch statements at all - is to use a Map with all possible grades as keys (since they are limited) and the mark ranges as values. For simplicity represent both as String for now.

Achieved through something like this:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Grades
{
    public static void main(String[] args)
    {
        Map<String, String> marks = new HashMap<String, String>();
        marks.put("A+", "90-100");
        marks.put("A", "85-89");
        marks.put("A-", "80-84");
        marks.put("B+", "77-79");
        marks.put("B", "73-76");
        marks.put("B-", "70-72");
        marks.put("C+", "67-69");
        marks.put("C", "63-66");
        marks.put("C-", "60-62");
        marks.put("D+", "55-59");
        marks.put("D", "50-54");
        marks.put("F", "0-49");

        Scanner scanner = new Scanner(System.in);
        String grade = scanner.next();

        if(marks.containsKey(grade))
        {
            String markRange = "Your mark range is " + marks.get(grade) + " %";
            System.out.println(markRange);
        }
        else
        {
            System.out.println("Unknown grade");
        }
        scanner.close();
    }
}

If you're really required to use switches and chars i would suggest the following:

we assume the mark will be the first char in the input and the modifier (+-) the second one.

for the outer switch we'll switch on the mark char and the cases will be your grades, like 'A', 'B', 'C' etc., inner loop similiar but the cases are '+' and '-'

initialize the modifier char with a value other than '+' or '-' so we can make use of the default case in the inner switches when there is no modifier char.

example:

char mark = grade.charAt(0);
char modifier = 0;

if(grade.length() > 1)
{
    modifier = grade.charAt(1);
}

switch(mark)
{
    case 'A':
        switch(modifier)
        {
            case '+': System.out.println("A+"); break;
            case '-': System.out.println("A-"); break;
            default: System.out.println("A"); break;
        }
        break;
    case 'B':
        break;
    case 'C':
        break;
}

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

There are several problems with your code:

  1. You should use input.next() not input.toString()
  2. switch(grade) must use Strings in the case, not int.
  3. switch("A") makes no sense, it will only work with "A". Same applies for switch("B") and similar.
  4. You cannnot have any statement between switch(...) { and case declaration, like this:

    switch ("B") {
        //line below must be removed.
        System.out.println("Your mark range is 73-76.99%");
        case 1: B(0)
    
  5. After fulfilling every case, you have to add a break statement, otherwise the code of the next case will be executed as well.

  6. You have code like this:

    A(0);
    A(1);
    B(1);
    

    This mean you're calling methods A, B... and on. This won't work if you don't have such methods (and I think you haven't written them).

Upvotes: 1

Related Questions