Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
two equal strings return false Posted by jenia2009 on 1 Sept 2010 at 7:09 PM
i want to make a type writer exercise program.
So the program outputs to the consol a letter and the user has to type that letter in.
Thats in a loop:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
int start=33;//character '!' in the ascii table
int end=126;//character '~' in the ascii table
Scanner scanner=new Scanner(System.in);
do{
int random=(int)(Math.random()*256);
while(!(random>start&&random<end)){
random=(int)(Math.random()*256);// generate a int between '!' and '!'
}
System.out.println((char)random);//print random character to the console
while(scanner.hasNext()){//this loop read the next character that i type in
String x=scanner.next();//my character that i type
String xx=Character.toString((char)random);
if(x.equals(xx)){//tries to match my input to the random character
//THIS IS WHERE IT ALL GOES WRONG, IN THE IF STATEMENT ABOVE. WHEN WHERE SUPPOSE TO BE TRUE, RETURN False
System.out.println("they are equal");
break;
}
System.out.println("WRONG");
}


}while(true);

}
}

please help.
Thank you for your kind concern.
jenia
Report
Re: two equal strings return false Posted by ColacX on 2 Sept 2010 at 2:07 PM
use string.matches(string s) instead of equals, i also recommend you to post further code with the
code tags
and notepad++ and rjtexteditor




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.