mercredi 14 septembre 2016

How can I print a users input that is inside of a loop, outside of the loop? (Java)

I just started taking a computer science class at school about 2 weeks ago. I am currently trying to create a sign up page. Where it asks you for your email and password and if it meets the requirements then it creates the account. Currently the only requirements are that the Email can't be above 24 characters and the Password can't be above 16 characters.

I have created most of it successfully but at the end once you have created the account I want it to print out the email that the user inputs. But I can't figure out how to print it out since the user inputs the email in a loop it can't get the email and print it. Any help would be appreciated. :)

import java.util.Scanner;
import java.lang.Math;
import java.io.*;
import java.util.concurrent.TimeUnit;
import static java.lang.System.*;
 
class practice2 {
    public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
     
       //Asks user to input an email
       System.out.print("Email:");
       String email = scan.nextLine();
       
    //If email is less than 24 it asks for a password, if it's more it goes to else
    if ((email.length() < 24)) {
      System.out.print("Password:");
      String pass = scan.nextLine();
      
      System.out.println("You are now signed up");
      
      //If email is greater than 24 it continues to the next loop
    } else {
    

      do {
        //Prints out "Maximum of 24 characters!", since the email was more than 24 characters
        if ((email.length() > 24)) {
          
        System.out.println("Maximum of 24 characters!");
        
        //Stops code for 2 seconds, so the user can see the message that was printed
        try {
          TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
        }
             //Asks user for email again
             System.out.print("\n\n\nEmail:");
             String email2 = scan.nextLine();
             
             /* If email2 is less than the 24 the code will continue to the next loop, 
                but if its still more than 24 characters it will reset this loop */
             if ((email2.length() < 24)) {
               break;
             }
        }
        
      
      } while ((email.length() > 24));  
      
      //Asks user for password
      System.out.print("Password:");
      String pass2 = scan.nextLine();
      
      //Declares the email2 string outside of the loop
      String email2 = "*****";
      
      //I created this just for looks
      String fakepassword = "*****";
      
          do {
            
        //if pass2 is greater than 16 it prints "Maximum of 16 characters!"
        if ((pass2.length() > 16)) {          
        System.out.println("Maximum of 16 characters!");
        
        //Stops code for 2 seconds, so the user can see the message that was printed
        try {
          TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
        }
        
             //Asks user for password again (If the password was too long)
             System.out.print("\n\n\nPassword:");
             String pass3 = scan.nextLine();
             
             //If pass3 is less than 16, it prints out "You are now signed up" and goes to next loop
             if ((pass3.length() < 16)) {
               
           //If email is less than 24 than it prints out the accounts details (For users who were succesful on first try)
            if ((email.length() < 24)) {
        System.out.println("\n\nAccount created.");
        System.out.println("Email: " + email);
        System.out.println("Password: " + fakepassword);
        //If email is more than 24 it prints out the accounts details (For users who didn't succeed on first try)
      } else {
        System.out.println("\n\nAccount Created.");
        System.out.println("Email: " + email2);
        System.out.println("Password: " + fakepassword);
      }
               break;
             }
        }
        
        else {
          break;
        }

        } while ((pass2.length() > 16));  


    }
}
}

Aucun commentaire:

Enregistrer un commentaire