Results 1 to 6 of 6
- 10-01-2011, 04:50 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Strange problem occurring with my code
Hello forum members!
My name is Steven. But I'm known as
Tech2011 online.
I am new to this forum, and I'm brushing on
my Java skills, since I have seem to
forgotten some of my Java skills. Please
excuse me if I'm asking a dumb question.
So anyways, I am trying to compare a scanner string (converted the scanner to a string using .toString();, and was then trying to compare the string the user entered, to the
correct string. My code shown below:
public static void main(String args[])
{
System.out.println("Please enter the password to access the program. If you enter an incorrect password, you will have to wait until the for loop is closed to start over.");
Scanner text=new Scanner(System.in);
String passwords=text.toString();
if (passwords=="correctpassword"){
System.out.println("You guessed correctly.");
}else{
System.out.println("You guessed INCORRECTLY");
for (int alphred=60000;alphred!=0;alphred--){
System.out.println(alphred);
}
}
}
}
When I compile and run this, the else statement is automatically executed, without user input. I am not sure why it's doing this, but you could compile and run this yourself and get the same result. I would most definitely appreciate any insight into the problem! Thanks in advance for your assistance!
Tech2011
-
Re: Strange problem occurring with my code
That's not how you use a Scanner object. You'll want to read the Scanner tutorial on how to use it before trying as you can't guess when it comes to programming.
- 10-01-2011, 06:56 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Strange problem occurring with my code
That's really not helpful. One, it does not give me any relevant information about my code, and two, I was not guessing and you're just saying that to piss me off and make me feel bad. Rude!
-
Re: Strange problem occurring with my code
I'm not trying to piss you off, but you are using a new class without first looking up how to use it and that's not how to learn programming. Please look at the tutorial to see how to use Scanner as it's all there. If you take the time to read the tutorial, you will not only learn how to fix your code, but will also learn many details about Scanner and Java that you wouldn't get otherwise. How you accept this answer will be up to you, but if you look at it in a positive light and follow my recommendations, you will be better off, trust me. Our goal is not to "fix your code" but to help you learn how to code -- and there's a big difference.
-
Re: Strange problem occurring with my code
The other reference you'll want to use is the API which you can find here: Scanner API. The general API is here: Java API
Hint: you'll want to call methods on your Scanner to have it read in the user's input. Look for methods that start with nextXx();
- 10-13-2011, 10:30 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Colorchooser - Strange Exceptions in simple code.
By spec8320 in forum AWT / SwingReplies: 3Last Post: 04-09-2011, 04:40 PM -
strange problem
By dinosoep in forum New To JavaReplies: 6Last Post: 05-01-2010, 10:28 AM -
Strange Problem
By Aseem in forum JDBCReplies: 5Last Post: 02-12-2010, 07:46 AM -
View the most occurring value from an arraylist
By frozensun in forum New To JavaReplies: 12Last Post: 11-01-2009, 06:13 PM -
strange code
By tghn2b in forum New To JavaReplies: 3Last Post: 12-22-2008, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks