Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Newbie, why is my program printing output twice?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Newbie, why is my program printing output twice?

    hey guys, recently opened a java book and started coding, it was fun

    having problem with this password checker that i tried my hand at

    it prints output twice

    import java.util.*;
     
    public class SimplePassword {
     
    	public static void main(String[] args) {
     
    		//variables
    		Scanner myScanner = new Scanner(System.in);
    		String password = "swordfish";
    		String userinput;	
     
    		//runs once and then repeatedly while the password check returns false
    		do{
    			System.out.println("Write password ");
    			userinput = myScanner.next();	
    			checkPW(password, userinput);
    		}while(checkPW(password, userinput) == false);
     
    	} //end main
     
    	//comparison method
    	public static boolean checkPW(String password, String userinput){
     
    		if(password.equals(userinput)){
    			System.out.println("Access granted");
    			return true;
     
    		}
    		else
    		{
    			System.out.println("Access denied.");
    			return false;
    		}
     
    	} //end checkPW
    }// end class

    example output:

    Write password 
    monkeys
    Access denied.
    Access denied.
    Write password 
    swordfish
    Access granted
    Access granted

    Can you explain why it does this to me please?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Newbie, why is my program printing output twice?

    How many times are you call the checkPW method in your loop? Twice! Once inside of the loop itself, and once in the while boolean condition. I suggest you call it only once.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    VikingCoder (November 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Newbie, why is my program printing output twice?

    Thank you very much! Changed the while to this:

    while(!userinput.equals(password));

    now it works just fine :3

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Newbie, why is my program printing output twice?

    Great, glad you've got it to work!

Similar Threads

  1. help with printing output in columns
    By jblankinship in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2012, 01:03 PM
  2. Help in making an output for this Graphic (newbie)
    By javarum in forum AWT / Java Swing
    Replies: 49
    Last Post: June 27th, 2011, 06:22 PM
  3. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 08:26 AM
  4. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM