Results 1 to 4 of 4
  1. #1
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Question A type racer game problem

    Hello everyone, just registered here to join my fellow java friends :D
    I started programming with java about a week ago, and I wanted to make a game where random words appear and you have to type them as fast as possible. Unfortunately seems like I can't get it to work - word generation is good but for some reason I can't check player's answer.
    Here is the code
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class Game {
    
    	
    
    	private static Scanner reader;
    
    	public static void main(String[] args) {
    		
    		{
    		  Games();
    		//  CheckAnswer();
    		}
    	}
    
    	
    
    	//private static void CheckAnswer() {
    		
    	//}
    
    	public static void Games() {
    		int maxlength = 3;
    		final String chars = "abcdefghijklmnopqrstuvwxyz";
    		String word = "";
    		while(word.length() < maxlength){
    			Random rand = new Random();
    			int x = rand.nextInt(26) ;
    			word = word + chars.charAt(x);
    		}
    			System.out.println("TYPE: " + word);
    			Scanner reader = new Scanner(System.in);  
    			String answer = reader.nextLine(); 
    			if(answer == word ) System.out.println("CORRECT");
    			else System.out.println("NOT CORRECT");
    		}
    	}
    Whatever I type in the console it says Not correct.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: A type racer game problem

    Whatever I type in the console it says Not correct.
    Use the equals() method to compare the contents of String objects, not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: A type racer game problem

    Quote Originally Posted by Norm View Post
    Use the equals() method to compare the contents of String objects, not the == operator.
    Wow, that was fast! Thanks, its solved my problem. I knew about equals method but I thought its same like ==. Anyways, lesson learned ;)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: A type racer game problem

    equals method but I thought its same like ==
    No, they are different.
    == tests if two variables have the same value. Mostly used with primitives, but can be used with reference variables to see if they refer to the SAME object.
    equals() executes code in an object that sees if the contents of that object is the same as the contents of another object. Later you will write you own versions of the equals method for your classes and it will be up to you to decide what fields in an object should be compared to see if two objects are equal.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: 04-15-2015, 09:19 PM
  2. Hangman Type Game
    By krissy992 in forum New To Java
    Replies: 2
    Last Post: 02-19-2013, 09:51 AM
  3. Risk type game
    By aleczandru in forum Java Gaming
    Replies: 1
    Last Post: 09-28-2011, 07:43 PM
  4. tetris type game--- boundary help
    By ftrengnr in forum New To Java
    Replies: 11
    Last Post: 11-09-2010, 10:00 PM
  5. Replies: 22
    Last Post: 04-21-2009, 05:40 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
  •