Computing.Net > Forums > Programming > java switch statement problem

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

Reply to Message Icon

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;
}
}



Sponsored Link
Ads by Google

Response Number 1
Name: DownRoll
Date: November 10, 2005 at 17:55:59 Pacific
Reply:

I am using a compiler called BlueJ, if that is helpful.


0

Response Number 2
Name: gimmpy225
Date: November 10, 2005 at 20:04:08 Pacific
Reply:

whats the error?

Gimps


0

Response Number 3
Name: DownRoll
Date: November 11, 2005 at 07:44:41 Pacific
Reply:

the error I get is:

Cannot resolve symbol - PhoneBook.containsKey(java.lang.String)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java switch statement problem

Logical error in Switch Statement.. www.computing.net/answers/programming/logical-error-in-switch-statement/6532.html

How to use Switch statement in vb www.computing.net/answers/programming/how-to-use-switch-statement-in-vb/6687.html

Switch statement code in C++ ..? www.computing.net/answers/programming/switch-statement-code-in-c-/6052.html