Results 1 to 6 of 6
  1. #1
    Tech2011 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Exclamation 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Tech2011 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strange problem occurring with my code

    Quote Originally Posted by Tech2011 View Post
    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!
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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();

  6. #6
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Strange problem occurring with my code

    Instead following lines :

    String passwords=text.toString();
    if (passwords=="correctpassword"){

    Try with these,

    String passwords=text.next();
    if (passwords.equals("correctpassword")){

    Best of luck.

Similar Threads

  1. Colorchooser - Strange Exceptions in simple code.
    By spec8320 in forum AWT / Swing
    Replies: 3
    Last Post: 04-09-2011, 04:40 PM
  2. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  3. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  4. View the most occurring value from an arraylist
    By frozensun in forum New To Java
    Replies: 12
    Last Post: 11-01-2009, 06:13 PM
  5. strange code
    By tghn2b in forum New To Java
    Replies: 3
    Last Post: 12-22-2008, 11:51 PM

Tags for this Thread

Posting Permissions

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