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: Issue with beginner program

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post Issue with beginner program

    Hello, I have created a program that is supposed to create a registration account for World of Warcraft. My main problem is when executing the program, it asks for the password TWICE mainly I believe because of this section of the code:

    public static void register(Scanner input)
          {  
     
             String testUN; 
             String testPW; 
     
     
     
             {
     
             System.out.println("Enter your username");
             testUN = input.nextLine();
     
             if (WOWAccount.okUsername(testUN) == false)
             {
                WOWAccount.addToFile(testUN);
                System.out.println("Username available");
             }
             else
             {
                System.out.println("Username is not available");
                register(input);
     
             }
             }

    When I enter a username that is listed on the users.txt file, it will kick me back and ask me for a username again after displaying a message saying, "username is not available". However, If I enter a username that is not taken, everything is fine and it will prompt for a password. After I enter the password, it will complete the registration, but then ask me for a password again.

    I believe the reason why that is, is because I have another code asking for the password shorty after the previous code. So my issue is that I would like to ask for the username again in the previous code and not call upon the whole register method which includes asking for the password also.

    This whole error ONLY occurs if you enter a username that is in use first. If you enter a username that is not in use, it will complete the program successfully!

    Here is the section of code that follows that I think contributes to the error by asking again for a password after the register method gets called due to entering a username that is not available.
    do
             {
                System.out.println("Enter a Password:");
     
                testPW = input.next();
     
     
             }while(WOWAccount.okPassword(testPW) == false);

    I am wondering how I can set up the program so that it doesn't ask for the password twice. I apologize if anything I stated is unclear and please ask me to clarify if that is the case. The program compiles fine and I have attached the driver along with the class.


    Driver program:
       import java.util.Scanner;
       public class TestWOWAccount
       {
       /**
       *Main Method
       */
          public static void main(String[] args)
          {
             Scanner input = new Scanner(System.in);
     
             do
             {
                helloMenu();   	
                String choice = input.nextLine().toUpperCase();
     
                if(choice.equals("REGISTER"))
                {
                   register(input);
     
                }
                else if(choice.equals("LOG ON"))
                {
                   log_on(input);						 				
                }
                else
                {           		
                   System.out.println("An un-recognized command has been received."
                      + "\nPress Enter to Continue:"); 
                }   	
             }while(true);
     
          }
     
    	/**
    	* HelloMenu
    	*/
          public static void helloMenu()
          {
             System.out.println("Please type in your selection:\n"
                + "Register Or Log on");	
          }
     
          static String filename = "users.txt";
     
    	/**
    	* register method
    	*/
          public static void register(Scanner input)
          {	
     
             String testUN; 
             String testPW; 
     
     
     
             {
     
             System.out.println("Enter your username");
             testUN = input.nextLine();
     
             if (WOWAccount.okUsername(testUN) == false)
             {
                WOWAccount.addToFile(testUN);
                System.out.println("Username available");
             }
             else
             {
                System.out.println("Username is not available");
    				register(input);
     
             }
             }
     
             do
             {
                System.out.println("Enter a Password:");
     
                testPW = input.next();
     
     
             }while(WOWAccount.okPassword(testPW) == false);
     
             input.nextLine();
     
     
          	// create an instance of the WOWAccount class 
          	// that passes data entered as arguments to constructor
          	//      
             WOWAccount user1 = new WOWAccount(testUN, testPW);
     
          // gets data from user1 and display it back
             System.out.println();
             System.out.println("Thank you, you have successfully created "
                + "a new account");
             System.out.println("Your username: "+ user1.getUsrName());
             System.out.println("Your password: "+ user1.getPassword()); 
     
          }
     
     
     
       /**
       * calls the Log On Method
       */
          public static void log_on(Scanner input)
          {
             System.out.println("Log On Method has been called.");
          }
     
     
       }

    Class:
       import java.util.Scanner;
       import java.io.*;  
     
       public class WOWAccount
     
     
       {
          private String username;
          private String password;
     
          public WOWAccount()
          {
             username = "DEFAULT";
             password = "DEFAULT";
          }
     
       /**
       *Constructor
       */
          public WOWAccount(String testUN, String testPW)
          {
             username = testUN;
             password = testPW;
          }
     
       /**
       *setUsrName methods accepts an argument for the username
       */
          public void setUsrName (String testUN)
          {
             username = testUN;
          }
     
       /**
       *setPassword methods accepts an argument for the password
       */
          public void setPassword (String testPW)
          {
             password = testPW;
          }
     
       /**
       *getUsrName method returns the username
       */
          public String getUsrName()
          {
             return username;
          }
     
       /**
       *getPassword method returns the password
       */
          public String getPassword()
          {
             return password;
          }
     
     
       /**
       *Checks password requirement for proper validation
       */
          public static boolean okPassword(String testPW)
     
          {
             int len = testPW.length();
             boolean PassOK = true;
     
     
             int upC, countU = 0;
             int lowC, countL = 0;
             int digC, countD = 0;
     
     
             if (len >= 6)
             {
     
     
                for (len = 0; len < testPW.length(); len++)
                {
                   if (Character.isUpperCase(testPW.charAt(len)))
                   {
                      countU++;
                   }
     
                   else
     
     
                      if (Character.isLowerCase(testPW.charAt(len)))
                      {
                         countL++;
                      }
     
                      else
     
                         if (Character.isDigit(testPW.charAt(len)))
                         {
                            countD++;
                         }
                }
     
                if ((countU > 0) && (countL > 0) && (countD > 0))
                {
                   PassOK = true;
                }
     
                else
     
                   PassOK = false;
     
     
     
             }
     
             else PassOK = false;
             return PassOK;
     
          }
     
     
     
          static String filename = "users.txt";
     
       	/**
       	*validates username is not in use
       	*/
          public static boolean okUsername(String testUN)
          {
     
     
             Scanner input = null;
             try
             {
                input = new Scanner(new File(filename));
     
                while(input.hasNext())
                {
                   String curUsername = input.next();
     
                   if (testUN.equals(curUsername)) 
     
                      return true;
                }
                return false;
             }
                catch(FileNotFoundException e)
                {
                   System.out.println("file does not exist");
                }
             return false;
     
          }
     
     
          public static void addToFile(String testUN)
          {
             PrintWriter output = null;
             try
             {
                FileWriter fwriter = new FileWriter(filename, true);
                output = new PrintWriter(fwriter);
     
                output.println(testUN);
             }
                catch(IOException e)
                {
                   System.out.println("ERROR!");
                }
             finally
             {
                if (output != null)
                   output.close();
             }
          }
    }


    Thank you
    Last edited by suxen; April 2nd, 2011 at 09:34 PM.


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Issue with beginner program

    The issue is in the 'else' block of the username check, I'd recommend doing something similar to what you are doing with the password check loop instead of using 'if else'. Let me know if you require more explanation.

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

    suxen (April 4th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Issue with beginner program

    Quote Originally Posted by JJeng View Post
    The issue is in the 'else' block of the username check, I'd recommend doing something similar to what you are doing with the password check loop instead of using 'if else'. Let me know if you require more explanation.
    Thanks JJENG, I actually got it working by doing something else. I'll try your suggestion as well. However, I do need help with one thing.

    If I want to attach a password to a specific user after he chooses a password, how would I go about doing that? So that I may use it in the LOGON method later when the user wants to sign in without registering. Thanks for the help!

  5. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Issue with beginner program

    It all depends how you'd want to design your system. For a simple system, you could try and add the username and password to the file at the same time. That would require editing to okUsername, you'd probably want to make a method like okLogin to check the file for the username, and if the corresponding password matches. This is a simple solution that doesn't scale very well, as you add more people, your worst case scenario is O(n), and file operations are not very quick. And since you're using a file, your program will tie up the file, thus not allowing the file to be accessed by other instances of your program. There are may other ways to do this, and many other things to consider that i think are outside the scope of your program and this post, I gotta goto work, I hope this helps, good luck.

Similar Threads

  1. Programming beginner
    By LatinaC09 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 01:33 PM
  2. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM
  3. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM
  4. Repeating program issue
    By Bill_H in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2009, 10:58 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM