Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Switches

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Switches

    Hello everyone. I am beginer in Java programming. Current chapter i am reading covers if,else and switch. I am doing a lot of hands on and as i read chapters i try to create diffrent codes. Sorry this is probably very simple for you guys but i cant figure it out. i already created similair project just using if but i wanted to do switch one.

    Thank you
    Attached Files Attached Files


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Switches

    Not sure what you're asking help on exactly.

    I think I get your issue though from looking at your code. If you want it to only do the case that you want it to and not go onto the next one if it meets it, then add a break clause after each case

    case 'A':
    System.out.println("You got A");
    break;

    Here's a java tutorial on how to do switches. The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switches

    Goodbyeworld, the switch is not working at all and on lines that gradel variable get assigned a char it says it is being unused

    --- Update ---

    I did forget break; tho thanks for reminding

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Switches



    Yeah, I shoulda seen that earlier. I was only looking at the switch part earlier.


    One problem is that you have the char variable gradel local to your if statements and hence it goes out of scope when your if statements end. That's probably causing an error.

    int grade = 85;
    char gradel = 'x';


    I might add that you may want to use && statements to your code so it will output that you got a B (as it goes right now, it will output that you got a C.)

    Also, using if , else if, else statements will help with your if part so it outputs the right answer.


    Each grade has a range. For A, you said it needs to be 90 or more. For B, you said it has be less than 90 AND greater than or equal to 80. For C, you said it has to be less than 80 AND greater than or equal to 70, for F, it has to be less than 70.


    Your code is doing nothing if the grade is 90 or 80 as it is written right now and is marking it as C as an 85 would fit in the B range but would also fit in the category of >= 70 so it's going into that block too and hence it's retuning a C as your grade when you give it an 85.


    Use if, else if, and else blocks to help make sure it only goes into one block. Use && when necessary to help specify both an upper and lower bound for the grade scores that fit that grade.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switches

    I am trying to make it so when grade falls within those ranges it assigns A B C F to them depending on what grade it.. i wrote code that achieves this by only if but just wanted to use switch.. i think i understand what you are saying about having gradel local now.. what is good solution?

  6. #6
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Switches

    You wanted to use switch to output something once it figured out the grade with your if statements or did you want to use switches to determine the grade

    (i.e. you pass an int grade value to the switch and have it assign the char variable a grade.)

    The second one is highly impractical in that you can't have a "range" in a switch and would have do something like


    switch(score)

    case 100:
    gradel = 'A';
    break;

    case 99:
    gradel = 'A';
    break;

    case 98:
    gradel = 'A';
    break;

    ........


    As for the char gradel and how to make it compile, you could do something like this:

    int grade = 85;
    char gradel = 'x';
    if (grade >= 90){
    gradel = 'A';
    }
    .............


    You need to initialize it otherwise it'll say something like "variable gradel might not have been initialized" when it reaches your switch statement.


    Also, as for using only if statements, you could do something like this and still get the correct ouput


    if (grade >=90)
    {
    gradel = 'A';
    }

    if (grade < 90 && grade >=80)
    {
    gradel = 'B';
    }

    ........





    Your code with the if statements will do this


    int grade = 85;
    if (grade >= 90){
    char gradel = 'A';
    }
    if (grade < 90);
    if (grade >= 80){
    char gradel = 'B';
    }
    if (grade < 80);
    if (grade >= 70){
    char gradel = 'C';
    }
    if (grade < 70){
    char gradel = 'F';
    }


    85 is not >= 90 so not 'A'

    85 is less than 90 so execute empty ; statement

    85 is >= 80 so gradel is 'B'

    85 is not less than 80 so don't go to empty ; statement

    85 is >= 70 so gradel is now 'C'

    85 is not less than 70 so not 'F'

    and, in the end, your code will output,

    "You got C."

  7. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    Jfrogz (October 6th, 2013)

  8. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switches

    Thank you this is what i have now. good news is errors are gone..however it is not functioning. I am pretty new to this. this is what i have right now i will try to make it work from information you gave me

    public class switches {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    int grade = 90;
    char gradel = 'X';
    if (grade >= 90)
    gradel = 'A';

    if (grade < 90)
    if (grade >= 80)
    gradel = 'B';

    if (grade < 80)
    if (grade >= 70)
    gradel = 'C';

    if (grade < 70)
    gradel = 'F';

    switch (gradel){
    case 'A':
    System.out.println("You got A");
    case 'B':
    System.out.println("You got B");
    case 'C':
    System.out.println("You got C");
    case 'F':
    System.out.println("You got F");

    }
    }

    --- Update ---

    This is the one i did using if only.. the only reason i wanted to do switch so i can understand and be able to implent it if i need to.. the if one was very straight forward and worked

    public class If {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    int grade = 85;
    if (grade > 89){
    System.out.println("You got A");
    System.out.println("Your grade is " +grade);
    }
    if (grade < 90){
    if (grade >= 80)
    System.out.println("You got B");
    System.out.println("your grade is " +grade);
    }
    if (grade < 80)
    if (grade >=70){
    System.out.println("You got C");
    System.out.println("Your grade is " +grade);
    }
    if (grade < 70){
    System.out.println("You got F");



















    }

    }
    }

Similar Threads

  1. newbie start java program with switches
    By fortune2k in forum Java Theory & Questions
    Replies: 4
    Last Post: November 1st, 2010, 05:13 PM
  2. switches and else if
    By silverspoon34 in forum Collections and Generics
    Replies: 1
    Last Post: November 11th, 2009, 04:09 PM