1

I'm new to programming, and am taking a class. One of my assignments involves using a switch statement, which is something we've only recently learned. I got most of my code, but for some reason, the switch statement won't work. The program compiles, and prompts the necessary inputs, but does not print any of the switch statements when I enter the values. Basically, I want to prompt the user to enter in the student number (any number), enter in the credits, and print the correct statement. Here is my code:

import java.io.*;
import java.util.*;
public class Prog222
{
    public static void main(String[] args)
    {
        for(int i = 1; i<=4; i++)
        {
            double freshmanC = 1.0;
            double sophomoreC = 2.0;
            double juniorC = 3.0;
            double seniorC = 4.0;
            Scanner kbReader = new Scanner(System.in);
            System.out.print("Enter student number "); //Enter 2352, 3639, 4007, and 4915 respectively
            int studentNumber = kbReader.nextInt();
            System.out.print("Enter credits "); //Enter 30.0, 29.9, 70, and 103.7 respectively
            double credits = kbReader.nextDouble();
            switch((int) credits)
            {
                case '1': //Below 30 credit
                System.out.println("Grade level code = " + freshmanC);
                break;
                case '2': //30 credits or more but less than 70 credits
                System.out.println("Grade level code = " + sophomoreC);
                break;
                case '3': //70 credits or more but less than 90 credits
                System.out.println("Grade level code = " + juniorC);
                break;
                case '4': //90 credits or more
                System.out.println("Grade level code = " + seniorC);
                break;
            }
        }
    }
}

I've worked on this for a few hours, but I can't really find where I'm going wrong with my switch statement since it won't print. I'd really appreciate the help! Thanks!

1
  • 1
    There is a difference between '1' and 1 in Java: the first one is a character constant that is equal to 49, while the second one is equal to 1. Commented Nov 13, 2013 at 19:05

3 Answers 3

5

Remove the quotes so you're comparing integers rather than character literals:

switch((int) credits) { 
  case 1:
     ...
Sign up to request clarification or add additional context in comments.

Comments

2

Well, you have two fundamental problems here. The first is that you're trying to compare int values with char values, meaning none of your cases will execute.

The second problem is that in order for a switch statement to execute the code in a specific case, it must exactly match the case. So if hypothetically you were using int instead of char as follows:

switch((int) credits)
    {
        case 1: //Below 30 credit
            System.out.println("Grade level code = " + freshmanC);
            break;
        case 2: //30 credits or more but less than 70 credits
            System.out.println("Grade level code = " + sophomoreC);
            break;
        case 3: //70 credits or more but less than 90 credits
            System.out.println("Grade level code = " + juniorC);
            break;
        case 4: //90 credits or more
            System.out.println("Grade level code = " + seniorC);
            break;
    }

It would never execute unless the value for credits was 1, 2, 3, or 4, respectively. A quick way to remedy this would be to divide credits by 30 and add one, so the follow would fix you up

int newCredits = ((int)credits/30) + 1
switch(newCredits)
    {
        case 1: //Below 30 credit
            System.out.println("Grade level code = " + freshmanC);
            break;
        case 2: //30 credits or more but less than 70 credits
            System.out.println("Grade level code = " + sophomoreC);
            break;
        case 3: //70 credits or more but less than 90 credits
            System.out.println("Grade level code = " + juniorC);
            break;
        default: //90 credits or more
            System.out.println("Grade level code = " + seniorC);
            break;
    }

As a quick note, I changed case 4: into default: so that if an individual has more than 120 credits, it will still classify them as a senior.

Edit

So I just realized that your credit hours don't increment in a uniform way (that's what I get for assuming that all colleges classify everyone the same). You'll have to find a way to get some sort of standardization for your credits value.

1 Comment

Go with this answer. This is the only one that factors in your comments of what your switch statement should be checking for.
1

You're switching on an integer, but you use char in your cases. So yes, none of these cases will ever be true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.