Results 1 to 15 of 15
  1. #1
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default how many ways can I use a switch statement

    I'm trying to come up with multiple ways to use a switch statement instead of the one way that was shown in the TUT
    I'm wondering if I can have the end user punch in a number and it goes exactly to the case the number that coincides with the specific case

    EX:
    Java Code:
    import java.util.Scanner;
    public class SwitchCase {
    public static void main(String arg[]){
    int Account1;
    int Account2;
    int Account3;
    Account1 = 1441;
    Account2 = 2441;
    Account3 = 3441;
    Scanner Info=new Scanner(System.in);
    System.out.println(Info.nextLine());
    switch (Account1){
    
    case 1441:
    System.out.println("Your Name is Chuck Thomas" );
    break;
    case 2441:
    System.out.println("Your Name is James Waverly");
    break;
    case 3441:
    System.out.println("Your name is Johnny Knoxville");
    default:
    System.out.println("Account not present");
    break;
    I want it to be possible for the user to punch in which case they want
    Last edited by Norm; 11-23-2011 at 09:55 PM. Reason: fixed ending code tag

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    What do you want the user to enter: The full number(4 digits) used in the switch or do you want it simpler like: 1,2, 3
    You can have more than one case: statement for a block of code.

    Ask the user the question, get his response as an int (or String and convert to int) and use that in the switch

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: how many ways can I use a switch statement

    In Java 7, you can also use Strings in a switch. The usual condition of cases being compile time constants still applies.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    So I would write the cin>> like this
    Java Code:
    import java.util.Scanner
    class name{
        public static void main(String arg[]){
           System.out.out.println("enter your four digit number);
              Scanner input = new Scanner (System.in);
                int Number;
                  Number = (input);
                   switch (Number){
    case 1:
    Last edited by jaytee; 11-23-2011 at 10:25 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    Does your code compile ok?
    Before posting, you'd save some time if you'd compile it and get rid of the compiler errors before posting.
    Also if you don't understand what the error message says, you could post the full text of the error message with your question.

  6. #6
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    it compiles but when I enter any of the 4 digit numbers it gives me the first case only
    (e.g. 2441
    2441
    Your Name is Chuck Thomas)
    only 1441 is supposed to be Chuck Thomas

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    Please post the code that you are currently executing.

  8. #8
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    This is the code I'm currently posting and it compiles, it just doesn't do exactly what I want it to do

    Java Code:
    import java.util.Scanner;
    public class SwitchCase {
    public static void main(String arg[]){
    int Account1;
    int Account2;
    int Account3;
    Account1 = 1441;
    Account2 = 2441;
    Account3 = 3441;
    Scanner Info=new Scanner(System.in);
    System.out.println(Info.nextLine());
    switch (Account1){
     
    case 1441:
    System.out.println("Your Name is Chuck Thomas" );
    break;
    case 2441:
    System.out.println("Your Name is James Waverly");
    break;
    case 3441:
    System.out.println("Your name is Johnny Knoxville");
    default:
    System.out.println("Account not present");
    break;

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    That code will not compile. For starters it is missing 3 ending }s

    What is the value of Account1 when you execute the switch statement?
    Add a println just before the switch statement to print out the value of Account1 so you can see.
    Last edited by Norm; 11-23-2011 at 10:40 PM.

  10. #10
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    I hope I followed your directions right but this is what I put before the switch statement
    Java Code:
    System.out.println(Account1);
    		switch (Account1){
    after it compiled the screen was blank until I punched in Account1
    and this is what popped up
    Java Code:
    Account1
    Account1
    1441
    Your Name is Chuck Thomas

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    Is that the correct way for the switch statement to work when Account1 has the value: 1441?

    You need to read a value in from the user and store it in Account1 to give the user control over what happens.

  12. #12
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    so
    Java Code:
    import java.util.Scanner;
    and
    Scanner Info=new Scanner(System.in);
    is not the way to read the value in?
    what is the correct syntax for that code?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    After you define a Scanner object, you must use one of the Scanner class's next... methods to read data into a variable.
    Look at the API doc for the Scanner class to see which method to use.
    Also there are lots of code samples on the forum. Search here for Scanner or use Google

  14. #14
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: how many ways can I use a switch statement

    where can I find the API docs?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: how many ways can I use a switch statement

    This site has doc for the SE classes:
    Java Platform SE 6
    Find the class in the lower left, click on it and the doc is displayed in the main window.

Similar Threads

  1. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 09:54 AM
  2. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM
  3. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  5. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:11 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
  •