I'm doing a virtual zoo in computer science and i'm stuck. We are using a guide to navigate through the zoo, but I can't figure out how to go without getting errors. When I run it it says "Null! Null, and I have no idea what to do. I am pretty new to coding so thank you for your help.
class Guide {
private String name;
int Selection;
int Selection1;
int Selection3;
int Selection2;
int choice;
Scanner inputScanner = new Scanner (System.in);
void welcomeusertozoo() {
System.out.println("Hello! Welcome to Cringe Jarin! A Zoo full of cringy creative creatures. Im Fred. What is your Cringy Name?");
name = inputScanner.next();
System.err.println("Ewww..... How cringy. Well, Welcome " + name + "! What creatures would you like to visit First?"
+ "\n 1.Ogres"
+ "\n 2.Vampires"
+ "\n 3.Rats \n\n" );
choice = inputScanner.nextInt();
creatureselection(choice);
}
void creatureselection(int Selection) {
switch (Selection) {
case 1: System.out.println("Plug up the nose and close those eyes! These creatures are Ugly! who Would you Like to visit...\n Please press 1 to go meet the commander himself, Ogre E Lee. \n Please press 2 to go meet the one and only Ogrewall Jackson. \n Please press 3 to meet the wonderful Mr.GreenBean. \n Please press 4 to meet the rebellious William Shrallace. \n\n");
choice = inputScanner.nextInt();
switch (choice){
case 1: System.out.println("Commander Ogre. E Lee Here! \n\n");
case 2: System.out.println("This is the impeccable, the amazing, one and only Ogrewall Jackson \n\n");
break;
case 3: System.out.println("This is the fashionable, the sexy, the fabulous GreenBean \n\n");
break;
case 4: System.out.println("This is the free, the bravest heart I know, the impecable William Shrallace \n\n");
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
}
break;
case 2:System.out.println("Dont have a scarf? Oh.... watch your necks! Who would you like to visit...\n Please press 1 to go meet the spooky Dio. \n Please press 2 to go meet the truly bone chilling Diego. \n Please press 3 to meet the bloodthirsty Big White Bat. \n Please press 4 to meet the Bold Mubat. \n\n");
choice = inputScanner.nextInt();
switch (choice){
case 1: System.out.println("Dio \n\n"); creatureselection(2);
break;
case 2: System.out.println("Diego \n\n"); creatureselection(2);
break;
case 3: System.out.println("Big White bat \n\n"); creatureselection(2);
break;
case 4: System.out.println("Bold Mubat \n\n"); creatureselection(2);
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(2);
}
break;
case 3:System.out.println("Pull out the Cheese but dont let it rip! These creatures are Cringy! Who would you like to visit...\n Please press 1 to go meet the Odd Ratty Weisau. \n Please press 2 to go meet the backstabbing Risa. \n Please press 3 to meet the permanently sad Dindin. \n Please press 4 to meet the sketchy Markymouse. \n\n");
choice = inputScanner.nextInt();
switch (choice) {
case 1: System.out.println("Ratty Weisau \n\n"); creatureselection(3);
break;
case 2: System.out.println("Risa \n\n"); creatureselection(3);
break;
case 3: System.out.println("Dindin \n\n"); creatureselection(3);
break;
case 4: System.out.println("Markymouse \n\n"); creatureselection(3);
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(3);
}
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(1);
}
}
void setName(String fred) {
}
}
What seems to be the problem?
Can you provide more information on what is the problem.
It seems to be working.
Are you creating a Guide object?
public class test {
public static void main(String[] args) {
Guide guide = new Guide();
guide.welcomeusertozoo();
}
}
After that everything seems to be working.
Just make sure that you have breaks on every case, just to assure consistency.
Hope I've helped.
This is the main :
public class ZooProject {
public static void main(String[] args) {
Rat RattyWiseau = new Rat ();
Ogre OgreELee = new Ogre();
Vampire Dio = new Vampire ();
Guide Fredtheguide = new Guide ();
//ask user for name
RattyWiseau.setName("Ratty Wiseau");
Dio.setName("Dio");
// Fredtheguide.setName("Fred");
OgreELee.setName("Ogre E Lee");
Fredtheguide.welcomeusertozoo();
System.out.println(OgreELee.getName());
}
}
Related
I am writing some tests for a Java method which contains a switch statements but it seems that the "default statement" does not work. I only accept: Yes, No, Maybe. Everything else should return Maybe. My test always return the user input, does not matter what the user typed, so I guess my switch statement is not correct.
I have tried to move the default statement on the top
#Override
public String choice(String ans) {
getChoice = ans;
switch (ans) {
case "Yes":
break;
case "No":
break;
default:
getChoice = "Maybe";
}
return getChoice;
}
Thank you!
Your switch should work... As Kyle tells you "what are you overriding ?"
But why a switch when you can perform an "if then else" ?
Public String choice (String choice) {
If (choice.equals("yes") || choice.equals("no") {
return choice;
else {
return "maybe";
}
}
Switch is fine for multiple choices
This is kinda of a Noobie what about I am gonna ask but I am trying to get my Program to work I do not know how to Ask a question in a text box hit the button and it outputs the answer; I Have been researching this for a while I know how to get everything else to work.
So a simple Console app example:
static void Main()
{
bool exit = false;
string response;
while (!exit)
{
Console.Write("Command ('Exit' to end): ");
response = Console.ReadLine();
switch (response)
{
case "Hey":
Console.WriteLine("Welcome");
break;
case "unicorns":
Console.WriteLine("...are awesome!");
break;
case "Exit":
exit = true;
break;
default:
Console.WriteLine("Unrecognized command!");
break;
}
}
Console.Write("Press Enter to Quit");
Console.ReadLine();
}
I want to link my combobox selected item with switch case which result in different result depend on option chosen. But return in this error :Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to java.awt.event.ItemListener. Can only one help me, thanks you so much!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1.addItemListener((ItemListener) jComboBox1);
int usertype = (int) jComboBox1.getSelectedIndex();
switch (usertype) {
case '1':
new ResidentView().setVisible(true);
this.dispose();
break;
case '2':
new OfficeClerkView().setVisible(true);
this.dispose();
break;
case '3':
new OfficeManagerView().setVisible(true);
this.dispose();
break;
}
I'm trying to make a simple bank account program to learn classes and OOP. As you may guess, I'm new to Java.
Anyways, my switch statement is not working. I'm trying to make each case based on an inputted string.
Scanner input = new Scanner(System.in);
System.out.println("Enter your name");
//the user enters "user1", "user2", or "user3".
String user = input.next();
//swtich time
switch (user) {
case "user1":
System.out.println("Your balance is" + user1.balance);
System.out.println("Your Account numer is" + user1.acctnum);
//shows the balance and account number for user1
case "user2":
System.out.println("Your balance is" + user2.balance);
System.out.println("Your Account numer is" + user2.acctnum);
case "user3":
System.out.println("Your balance is" + user3.balance);
System.out.println("Your Account numer is" + user3.acctnum);
}
You're missing a break statement at the end of each case.
case "user1":
System.out.println("Your balance is" + user1.balance);
System.out.println("Your Account numer is" + user1.acctnum);
//shows the balance and account number for user1
break;
Without the break statement, all of these statements will be executed
Docs
You can use Strings in switch statements if you are using Java 7 or above, otherwise you can't
You can enumerate the string, and then use switch.
P.S: Please search around a bit and then post questions here ;) Your question is similar to this one (for ex - there are plenty of answers out there)
https://stackoverflow.com/a/338284/878170
You are missing break; statement and hence all statements after selected case will be executed.
Here's an example of switch-case [1]
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
[1] http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
switch(2){
default : System.out.println("I am Default block");
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
}
The output : in 2
and if
switch(2){
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
default : System.out.println("I am Default block");
}
And the output is: in 2
I am Default block.
Does the positioning of the default statement behaves differently?
It's not the position of the default block, it's the use (or non-use in your case) of the break statement.
Once a case has been matched, the default behaviour is for the code to cascade through the rest of the cases.
To achieve the behaviour you desire, try
switch (2)
{
case 1:
System.out.println("in 1");
break;
case 2:
System.out.println("in 2");
break;
default:
System.out.println("I am Default block");
break;
}
Kathy Siera SCJP book do say that position of default: matters and if present in between the case statements, it follows the fall-through.
But practically it does not follow.
int a = 10;
switch (a)
{
case 5:
System.out.println("Its 5");
default:
System.out.println("Its default");
case 10:
System.out.println("Its 10");
case 20:
System.out.println("Its 20");
}
Output:
Its 10
Its 20