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 6 of 6

Thread: Problem with nested switch code.

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Problem with nested switch code.

    I can't understand why the runtime appears to be completely ignoring the nested switch (act). I run the program and everything else runs fine but it's as if that part of the code isn't even there. It gets as far as the test code in the first switch (clr) case '3': but for some reason, no further.

    Console follows the code.

    // an easy to use program to hold snooker match scores and feed detailed statistics to the user
    import java.util.Scanner;
     
    public class SnookerStats{
     
    // interface to take points for player if ball is potted---------------------------------------
    interface Score{                             // top level interface class for colour selection
       public abstract int take(int point);               // abstract method
    }
     
    // interface to give points to other player if ball is fouled---------------------------------
    interface Foul{                              // interface for points for fouls
       public abstract int give(int point);
    }
     
    static class GreenClass implements Score, Foul{
     
       public int take(int point){                // method to return point value for green ball potted
          int r = point + 3;
          return r; 
       }
     
       public int give(int point){
          int r = 4;
          return r;
       }
    }
     
       public static void main(String args[]){
     
          Scanner sc = new Scanner(System.in);
          int temp, brk=0;                                              // temp holds number of frames, brk holds current break tally
          int [][] blsprfrm;                                          // 2D array for each ball per frame
          int wht, ylw, grn, brw, blu, pnk, blk;                      // argument to count number of balls potted
          int scwht, scylw, scgrn, scbrw, scblu, scpnk, scblk;        // if potted, collects score, if fouled, collects points for other player
          boolean active1 = false, active2 = true;                    // active break yes or no
          char clr, act;                                              // option variables to hold colour and action upon colour
          Score green = new GreenClass(); 
     
     
          // request number of frames from user 
          System.out.println("Number of frames to play: ");
          temp = sc.nextInt();
     
          // multidemensional array _ balls potted per frame
          blsprfrm = new int [temp][7];          // temp collects number of frames from user, 7 is number of ball types on table
     
          System.out.println("Number of frames to play: " + blsprfrm.length);
          System.out.println("Number of ball types on table: " + blsprfrm[0].length); 
     
     
          // ask which ball player is striking
          System.out.println("Select colour:\n1 : White\n2 : Yellow\n3 : Green\n4 : Brown\n5 : Blue\n6 : Pink\n7 : Black ");
          clr = sc.next().charAt(0);
     
          // player pot or safety
          System.out.println("Select from the following options:\nP : Pot\nM : Miss\nS : Safety\nF : Foul");
          act = sc.next().charAt(0);
     
          System.out.println("Colour selected: "+ clr);
          System.out.println("Action selected: "+ act);
     
          switch (clr){
     
             case '3':
                System.out.println("Green ball selected.");
                switch (act){
                   case 'P':
                      brk = green.take(brk);
                      System.out.println("Points for green ball: "+ brk);    
                   break;
                }
     
             break;
          }      
       }
    }

    Number of frames to play: 
    3
    Number of frames to play: 3
    Number of ball types on table: 7
    Select colour:
    1 : White
    2 : Yellow
    3 : Green
    4 : Brown
    5 : Blue
    6 : Pink
    7 : Black 
    3
    Select from the following options:
    P : Pot
    M : Miss
    S : Safety
    F : Foul
    p
    Colour selected: 3
    Action selected: p
    Green ball selected.


    --- Update ---

    Rookie error.

    I know switch cases are case sensetive but I mistakingly thought that I had tested that.

    It was a higher capital P in the case condition.
    Last edited by FightingIrishman; January 11th, 2022 at 03:29 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with nested switch code.

    What do you expect to happen after the message: Green ball selected. is printed?
    What values in what variables do you expect to control the program's execution flow?
    Do those variables have the expected values?

    The switch statements' default case is very useful to catch unexpected values. Add a default: to all the switch statements and have a print statement print out the value of the variable with the unexpected value. EG clr and act
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Problem with nested switch code.

    Green ball selected is just test code. The case of ball selection does nothing until an action for that ball is selected in the nested switch. I'm going to use a default case for error messages in the switch statement.

    I'm using two Interface classes called Score and Foul. Potting a ball will take the value of the overriding method for that particular ball colour while fouling that ball colour will override the Foul Interface and give points to the opponent instead. I'm doing it with interface classes for practise but I assume that final int variables for holding the value of each ball colour and the value for each foul would be cleaner code, would that be right?

    I don't know if you're familiar with the game of snooker for this to make sense, but when the points scored go past 74, I'm going to make a request to the player with least points to concede or not, then I'm thinking that another boolean variable alternating between true and false can alternate between player 1 and player 2 for me with an if else structure. Does that seem practical?

    I thought a snooker scoreboard would be an easy application but it's quite deep when I get into it. In my mind I would hope to have it run with only 3 or 4 buttons on a smart phone and still provide rich content but we shall see.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with nested switch code.

    final int variables for holding the value of
    Yes that is one way.
    Another is to use an enum

    boolean variable alternating between true and false
    Yes that could work.
    To flip a boolean use the ! operator: bool1 = !bool1; // flip the boolean
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Problem with nested switch code.

    Another is to use an enum.
    I haven't come across enum yet but it's ahead of me in the book I'm reading.


    To flip a boolean use the ! operator: bool1 = !bool1; // flip the boolean
    Handy. Cheers!

  6. #6
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem with nested switch code.

    To rectify the issue, ensure that the case in your switch statement matches the input exactly. Since 'act' is 'p', the case condition in the switch statement should also be 'p':

    ```java
    switch (act){
    case 'p':
    brk = green.take(brk);
    System.out.println("Points for green ball: "+ brk);
    break;
    }
    ```

    This adjustment should help you overcome the hurdle in your Java assignment, allowing the nested switch statement to execute correctly. If you encounter further challenges or require help with programming assignment, seeking guidance from resources like ProgrammingHomeworkHelp.com can provide valuable insights and support.

Similar Threads

  1. Replies: 1
    Last Post: December 8th, 2021, 11:01 AM
  2. nested switch case (import java.io.*;)
    By arundhati dhar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2014, 10:48 AM
  3. Switch case with nested if inside
    By deadlynerd in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 16th, 2014, 04:01 AM
  4. [SOLVED] Nested Switch Statement Fall Through
    By Nate in forum Loops & Control Statements
    Replies: 6
    Last Post: July 19th, 2012, 01:49 PM
  5. a problem with a code... (for loop and an "if" nested..)
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 12:59 PM