0

Monster Database

Hey guys, I'm super new to Java and I'm just attempting to exercise my limited knowledge to retain stuff but one thing I'm struggling with is this article of coding: import java.util.Scanner; public class Monsters{ static Scanner userInput = new Scanner (System.in); public static void main (String []args){ System.out.println("What would you like to know about?"); System.out.println(" *Monsters"); String selection = (userInput.next()); if (selection.equalsIgnoreCase("Monsters")){ monsters(); } } public static void monsters(){ System.out.println("What would you like to know about Monsters?"); System.out.println(" * Where do Monsters come from?"); System.out.println(" * Where do Monsters sleep?"); System.out.println(" * Where do Monsters live?"); System.out.println(" * What do Monsters eat?"); String a = ("Where do Monsters come from?"); String b = ("Where do Monsters sleep?"); String c = ("Where do Monsters live?"); String d = ("What do Monsters eat?"); String answer =(userInput.next()); if(answer.equalsIgnoreCase(a)){ System.out.println("They come from underneath the bed!"); } if(answer.equalsIgnoreCase(b)){ System.out.println("They sleep in the closet!"); } if(answer.equalsIgnoreCase(c)){ System.out.println("In the attic!"); } if(answer.equalsIgnoreCase(d)){ System.out.println("Carrots, strangely enough."); } } } So this is where I'm at. The main method runs appropriately (It gives you the selection " *Monsters" and when you type it in, it therefore runs the Monsters method. My issue lies within the Monsters method itself in that whenever you type in "Where do Monsters come from?" it simply terminates the program instead of running the if statement. What am I doing wrong?

19th Nov 2016, 11:06 PM
Skyler Joe Boles
Skyler Joe Boles - avatar
3 Réponses
0
you could try turning your if conditions into an "if, else if" condition and then at the end add an "else" condition to print out "error" this might help you find where the problem lies.
20th Nov 2016, 1:18 AM
steven trippier
steven trippier - avatar
0
I've actually since done that, but it still doesn't recognize what I'm typing. I think it's because I'm typing in complete sentences and it doesn't know how to utilize that? I know I can use numbers, but I'd like to retain the full sentences.
20th Nov 2016, 3:45 AM
Skyler Joe Boles
Skyler Joe Boles - avatar
0
I think there is a method I'm the string class to trim() your input which removes all white spaces I'm not sure if it's called trim but it's similar. you would have to make tour equalsIgnoreCase() arg use no spaces
20th Nov 2016, 10:07 AM
steven trippier
steven trippier - avatar