Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default 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

    Java 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:
    Java Code:
    That novel is not on the list
    ***MY CODE***

    Java 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:");
    
            }
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,368
    Rep Power
    24

    Default

    You can switch on a char, which is (sort of) what you're reading in from the user.

Similar Threads

  1. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 03:22 PM
  2. Demonstration of the switch statement
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  3. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 04:48 AM
  4. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06: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
  •