Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
java switch statement problem
Name: DownRoll Date: November 10, 2005 at 17:54:01 Pacific OS: XP CPU/Ram: Pentium M, 512mb
Comment:
Hello, I'm relatively new to java and I've got a problem with the switch statement. What I'm trying to do is inside the body of the switch im trying to check what a user inputs and return a Sys.out message if the user's input is not in my HashMap. I'm using containsKey() after an if statement, but I keep getting an error when compiling. The problem is at case 1. Here's what I've got so far.
import java.util.ArrayList; import java.util.Iterator; import java.util.HashMap; /** * Write a description of class RunPhoneBook here. * * @author (your name) * @version (a version number or a date) */ public class RunPhoneBook {
public static void main(String[] args) { InputReader reader = new InputReader(); PhoneBook phonebook = new PhoneBook(); String input, name, phone; int selection; boolean finished = false; while(!finished) { showMenu(); selection = getSelection(); switch (selection) { case 1: System.out.println("What is your friend's name? "); name = reader.getInput(); name = name.trim(); name = name.toLowerCase(); //if (phonebook.containsKey( phonebook name ) ) == null) //{ //System.out.println("Phone book already contains this value, you cannot add."); //} System.out.println("What is your friends phone number? i.e. XXX-XXX-XXXX"); phone = reader.getInput(); phonebook.addFriend(name, phone); break; case 2: System.out.println("Which friend would you like to delete?"); name = reader.getInput(); name = name.trim(); name = name.toLowerCase(); phonebook.removeFriend(name); break; case 3: System.out.println("Which friend would you like to search for?"); name = reader.getInput(); name = name.trim(); name = name.toLowerCase(); if(containsKey( name )) { System.out.println("hi"); } break; case 4: case 5: case 6: finished = true; break; default: } }
} public static void showMenu() { System.out.println("\n\t Menu"); System.out.println("\t\t 1. Add friend into phone book;"); System.out.println("\t\t 2. Remove friend from phone book;"); System.out.println("\t\t 3. Search friend's phone number;"); System.out.println("\t\t 4. Change friend's phone number;"); System.out.println("\t\t 5. Print friend information (optional);"); System.out.println("\t\t 6. Quit."); System.out.println("\n"); } public static int getSelection() { System.out.println("Please make a selection: "); InputReader reader = new InputReader(); int menuChoice = Integer.parseInt(reader.getInput()); return menuChoice; } }
Summary: Iam facing a problem in Switch statement. The problem is that i will ask to integer from the user & if the user want to "+" , "-" or "*" operator the switch case will work but in my coing it only as...
Summary: Iam using vb6 .Please can u write any code which can guide me that how to use Switch statement.I shall be so much thankfull if the code would be also execute. For example i have four option buttons...