Results 1 to 7 of 7
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    175
    Rep Power
    0

    Default forloops inside of switch statements

    This is what I'm trying to do;

    Java Code:
    import java.util.Scanner;
    class test{
    public static void main ( String[] args ) {
        Scanner scan = new Scanner ( System.in );
        int choice = scan.nextInt();
        
        switch (choice) {
          for(int i = 0; i < 10; i++) {
            case i:
              System.out.println("You chose " + i + "!");
              break;
          }
          default:
            System.out.println("Error");
            break;
        }
    }
    }
    How is this doable?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: forloops inside of switch statements

    How is this doable?
    Please explain what you are trying to do. What you've coded does not make sense.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: forloops inside of switch statements

    Clearly that is syntactically incorrect. Are you trying to dynamically create the case statements upon which to switch?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    175
    Rep Power
    0

    Default Re: forloops inside of switch statements

    Yes, jim, you have correctly perceived what I am trying to do.

    I'm trying to dynamically create those cases, as I have a bunch of them and I want to use the value from the forloop in the case statement as well.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: forloops inside of switch statements

    Source statements for java are created by the programmer using an editor. The compiler needs to have them in the source so it can create the class file.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: forloops inside of switch statements

    You clearly can't do it the way you have shown. But you could create an interface and create multiple instances and store them in a list.
    Then you can use the user input to select the appropriate instance from the list and invoke its method(s). But if the method contents are all different, as the case statement might be, then you still have to type them in.

    Regards,
    Jim
    Last edited by jim829; 03-30-2014 at 05:36 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    175
    Rep Power
    0

    Default Re: forloops inside of switch statements

    Yup, obviously my way doesn't work, as it won't compile. I know, I've tried it.


    Thanks for the answers. I may try your idea Jim.

Similar Threads

  1. Trying to display switch statements
    By CupONoodles in forum New To Java
    Replies: 1
    Last Post: 03-20-2012, 05:22 AM
  2. Multiple try & catch statements within a switch
    By Martyn in forum New To Java
    Replies: 21
    Last Post: 01-12-2012, 09:24 PM
  3. switch statements & conditionals
    By linkxs in forum New To Java
    Replies: 7
    Last Post: 09-25-2011, 06:17 AM
  4. switch statements
    By jim01 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 10:52 PM
  5. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •