Results 1 to 3 of 3
  1. #1
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Searching array elements for keywords

    Hi I have a simple question and answer game which I am trying to improve. At the moment everything is working how I'd like functionality wise.

    I have 2 string arrays 'questions' and 'answers'. When the correct word is typed in exactly the same, it works. Here's my code:

    Java Code:
    import java.util.Scanner;
    
    public class MyProgram {
    
         public static void main(String[] args) {
    	
    		 arraysMethod();
    		 
    	 } 
         
    	 public static void arraysMethod() {
    	 
    	     String input;
    		 int tracker = 0;
    		 
    	 
             String[] questions = new String[11];
    		 
    		 questions[1] = "What is the largest continent?";
    		 questions[2] = "Which animal has black and white stripes?";
    		 questions[3] = "What is a skeleton made of?";
    		 questions[4] = "Where do bats live?";
    		 questions[5] = "Where does a king and queen live?";
    		 questions[6] = "What colour does a rainbow start with?";
    		 questions[7] = "What sport does Wayne Rooney play?";
    		 questions[8] = "What animal goes 'oink oink'?";
    		 questions[9] = "Which planet is the nearest to Earth?";
    		 questions[10] = "What colour is a banana?";
    		 
    		 String[] answers = new String[11];
    		 
    		 answers[1] = "asia";
    		 answers[2] = "zebra";
    		 answers[3] = "bone";
    		 answers[4] = "cave";
    		 answers[5] = "castle";
    		 answers[6] = "red";
    		 answers[7] = "football";
    		 answers[8] = "pig";
    		 answers[9] = "moon";
    		 answers[10] = "yellow";
    		 
    		 for (int x=1;x<=questions.length-1;x++) {
    
             System.out.println(questions[x]);
    
             Scanner scan = new Scanner(System.in);
    		 input = scan.nextLine();
    		 
    		     if (input.equals(answers[x])) {
    		     
    			     System.out.println("Correct!");
    				 tracker++;
    		     }
    		     else {
    			     System.out.println("Wrong!");
    			 }
    		     
    		 }
    		 
    		 System.out.println("You scored " + tracker + "/10.");
    		 
    		 
    		
    	 }
    	 
    }
    Basically, when the user enters and extra letter, for instance in question 3, if the user enters 'bones' instead of 'bone' (the actual answer stored in the answers array), it tells them that is wrong. How could I search my answers array for keywords?

    I hope I have put my point across well and look forward to any feedback you may have.

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You could use the contains method. You may also be able to more easily write your code by creating some classes. for questions and answers. Which can have a question and a list of acceptable answers. If the user guesses one of the acceptable answers, he gets it correct; otherwise it's false.

  3. #3
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    That sounds much too complicated but in regards to the contains method, i'll look that up. Is it possible to store multiple keywords in an index, then use subString()?

Similar Threads

  1. Searching through Array of Objects
    By coopc in forum New To Java
    Replies: 6
    Last Post: 04-26-2011, 04:32 PM
  2. Replies: 3
    Last Post: 03-15-2011, 01:24 PM
  3. Replies: 0
    Last Post: 02-25-2011, 09:13 AM
  4. Searching and comparing Array elements
    By jmanswrd in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 07:06 AM
  5. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 10:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •