1

i need to know whats wrong with this code, every time i run it the answer is always true (even if its false), if anyone knows a shorter way to write this code...also helps :) thk

Here is the code of the program:

import java.io.Console;
import java.util.Random;
import java.util.Scanner;
public class texting {

    public static void main(String[] args) {
        int le = 1;
        int fail = 0;

       System.out.println ("Writting Check");
       System.out.println("Write The Lettes Fast you can and try to not worng, you have 60 sec");
       System.out.println("Press Enter To Continue");
        while (fail < 3)
        {
            String random = "";
            Random r = new Random();
            String chars = "abcdedfghijklmnopqrstuvwxyz";
            int time=60;
            int lives = 3-fail;
            System.out.println("Live Remiend:" + lives);

                for (int i = 0; i < le; i++)
                {
                    random += chars.charAt(r.nextInt(chars.length()));

                }
                System.out.println("Write The letters and prees enter");
                System.out.println(random);
                Scanner sc = new Scanner(System.in);
                String answer = sc.nextLine();
                System.out.println(random != answer);

                if (random != answer)
                {
                    System.out.println("Text Is Worng Try Again");
                    fail++;
                }
                else
                {
                    le++;

                }


        }


    }
}
user2911214
  • 21
  • 1
  • 4
  • Rename your question: its more about string comparison than char generation. (don't know if you can though) – johan d Oct 23 '13 at 12:12
  • Before you do the test if(random != answer) try output the value of these variables and see what is going on, I suspect there are newline characters in the input (without having spent too much time on your code) – avrono Oct 23 '13 at 12:12
  • @avrono ...why would that be? It's clearly because of `!=` instead of `!` `.equals()`. – tckmn Oct 23 '13 at 12:13

3 Answers3

3

You cannot compare strings with == or !=. You'll need to use the .equals() method. Strings are objects, so comparing Strings with ==, you'll be comparing if they refer to the same object or not, not their content.

String a = "abc";
String b = "abc";
System.out.println(a == b);    //prints false, a and b are 2 different objects
System.out.println(a.equals(b));    //prints true, the content of both string objects are equal;
b = "def";
System.out.println(a);    //prints "abc"
b = a;
System.out.println(a==b);    //prints true, a and b refer to the same String object
System.out.println(b);    //prints "abc";

When dynamically building a string, it's better to use a StringBuilder, instead of just concatenating Strings.

And as a third: a character is nothing more than a representation of a number.

// 'a'=97, 'z'=122
Random r = new Random();
StringBuilder randomString = new StringBuilder();
for (int i = 0; i < le; ++i) {
    int j = 97 + r.nextInt(122-97);
    char c = (char) j;
    randomString.append(c);
}
randomString.toString();
2

Try this:

if (!random.equals(answer))
     {
        System.out.println("Text Is Worng Try Again");
        fail++;
     }

In Java, one of the most common mistakes newcomers meet is using == and != to compare Strings. You have to remember, == and != compares the object references, not the content.

Linga
  • 10,379
  • 10
  • 52
  • 104
0

I think replace the line with

 if (!random.equals(answer))

instead of this

if (random != answer)
vinay Maneti
  • 1,447
  • 1
  • 23
  • 31