Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Naming a Switch Statement? Help!

    Hi,

    I have come across this question during my school assignment, and would appreciate some help since this is a new topic for me, I have provided the code I have written so far at the bottom. Basically I'm trying to name my 'cases' so a book can be entered rather than a number as in conjunction with switch cases.

    1. Switch to Philip K Dick books

    Code:
    Program Names : DickBooks.java
    Example Input/Output: 
    A A Scanner Darkly
    V Valis
    O Our Friends From Frolix 8
    U Ubik
    D Do Androids Dream of Electric Sheep?
    
    Choose a novel from menu above:
    u
    You chose "Ubik"
    This is a main/driver class.
    Use a switch statement to pick a novel by entering the correct character.
    • Allow the user to enter upper or lower case.

    If the novel is not on the list the program should output this exact string including a newline on the end:
    Code:
    That novel is not on the list
    ***MY CODE***

    Code:
    package assignment3;
    
    import java.util.Scanner;
    
    public class DickBooks {
    
        public static void main(String[] args) {
    
            int novel;
    
            Scanner daniel = new Scanner(System.in);
    
            System.out.print("Choose a novel from menu above:");
            novel = 1;
    
            System.out.println("You chose\n" + novel);
    
    
            switch (novel) {
                case 1:
                    System.out.println("A Scanner Darkly");
                    break;
                case 2:
                    System.out.println("Valis");
                    break;
                case 3:
                    System.out.println("Our Friends From Frolix 8");
                    break;
                case 4:
                    System.out.println("Ubik");
                    break;
                case 5:
                    System.out.println("Do Androids Dream of Electric Sheep?");
                    break;
                default:
                    System.out.println("That novel is not on the list");
    
    
                    System.out.println(scan.nextLine());
                    System.out.println("Choose a novel from menu above:");
    
            }
        }
    }
    Last edited by danielinthebed; 03-24-2011 at 04:19 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'm not sure exactly what your question here is.
    You should be able to switch case on a primitive value. You cannot switch case on a string. So either a numerical list or a char list would be fine. If you choose a char list though, you will run into size limitations so you wouldn't be able to grow beyond the size of the charset in use. Given the example you have, it appears the desire should be to construct a list and allow the user to provide the first char of the title. This would then need switching against a double case for each, since case calls are sensitive when comparing chars. It also means that you cannot have two titles that start with the same letter.

    The code you have doesn't really do a lot. All it will do is print out "A Scanner Darkly" and end. You need to scan input from a client prior to the switch. Default should only indicate that the book is not in the list.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    A small code example to go with Fou-Lu's comment that may help you.

    PHP Code:
    import java.util.Scanner;

    public class 
    MainCLass {
      static 
    Scanner sc = new Scanner(System.in);

      public static 
    void main(String[] args) {
        
    System.out.print("Enter the package code: ");
        
    String s sc.next();
        
    char p s.charAt(0);

        
    String details "";

        switch (
    p) {
        case 
    'E':
        case 
    'e':
          
    details += "\tE...\n";
        case 
    'D':
        case 
    'd':
          
    details += "\tD...\n";
        case 
    'C':
        case 
    'c':
          
    details += "\tC...\n";
        case 
    'B':
        case 
    'b':
          
    details += "\tB...\n";
        case 
    'A':
        case 
    'a':
          
    details += "\tA.\n";
          break;
        default:
          
    details "That's";
          break;
        }
        
    System.out.println(details);
      }



  •  

    Posting Permissions

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