Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-24-2011, 04:14 PM   PM User | #1
danielinthebed
New to the CF scene

 
Join Date: Dec 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
danielinthebed is an unknown quantity at this point
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..
danielinthebed is offline   Reply With Quote
Old 03-24-2011, 06:16 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 03-29-2011, 06:03 PM   PM User | #3
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
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);
  }

Gox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:03 AM.


Advertisement
Log in to turn off these ads.