Question

Convert this code written in Python into Java: valid = False; while(valid == False):    username...

Convert this code written in Python into Java:

valid = False;

while(valid == False):

   username = input ("Please enter a username:")

   length = len(username);

   if(length < 8 or length > 15):

       print("Username must be between 8 and 15 characters.")

   else:

      

       if(username.isalnum() == False):

           print("Username must contain only alphanumeric characters.");

       else:

           count = 0;

           inx = 0;

           for ch in username:

               if(ch.isdigit() and inx == 0):

                   print("The first character in your username cannot be a digit")

                   count = -1

                   break

               if(ch.isdigit() and inx > 0):

                   count = 1

                   break

               inx = inx+1

           if(count == 1):  

               for ch in username:

                   if(ch.islower()):

                       count = 2

                       break

                  

           if(count == 2):  

               for ch in username:

                   if(ch.isupper()):

                       count = 3

                       break

          

           if(count == 0):      

               print("Your username must contain at least one digit")  

                              

           if(count == 1):      

               print("Your username must contain at least one lowercase character")  

              

           if(count == 2):

               print("Your username must contain at least one uppercase character")

           if(count == 3):

               valid = True  

if(valid == True):

   print("Your username is valid!")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        boolean valid = false;
        int count, inx;
        while(valid == false){
            System.out.print("Please enter a username: ");
            String username = scan.next();
            int length = username.length();
            if(length < 8 || length > 15){
                System.out.println("Username must be between 8 and 15 characters.");
            }
            else{
                if(username.matches("[A-Za-z0-9]+") == false){
                    System.out.println("Username must contain only alphanumeric characters.");
                }
                else{
                    count = 0;
                    inx = 0;
                    for(int i = 0; i < length; i++){
                        if(Character.isDigit(username.charAt(i)) && inx == 0){
                            System.out.println("The first character in your username cannot be a digit");
                            count = -1;
                            break;
                        }
                        if(Character.isLowerCase(username.charAt(i)) && inx > 0){
                            count = 1;
                            break;
                        }
                        inx = inx + 1;
                    }
                    if(count == 1){
                        for(int i = 0; i < length; i++){
                            if(Character.isLowerCase(username.charAt(i))){
                                count = 2;
                                break;
                            }
                        }
                    }
                    if(count == 2){
                        for(int i = 0; i < length; i++){
                            if(Character.isUpperCase(username.charAt(i))){
                                count = 3;
                                break;
                            }
                        }
                    }
                    if(count == 0){
                        System.out.println("Your username must contain at least one digit");
                    }
                    if(count == 1){
                        System.out.println("Your username must contain at least one lowercase character");
                    }
                    if(count == 2){
                        System.out.println("Your username must contain at least one uppercase character");
                    }
                    if(count == 3){
                        valid = true;
                    }
                }
            }
        }
        if(valid){
            System.out.println("Your username is valid!");
        }
        scan.close();
    }
}

SAMPLE OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Convert this code written in Python into Java: valid = False; while(valid == False):    username...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert this code written in Python to Java: username=input("please enter a username") password=input("please enter a password:")...

    Convert this code written in Python to Java: username=input("please enter a username") password=input("please enter a password:") def is_a password(password):     count_uppercase, count_lowercase= 0, 0     for characters1 in password:         if characters1.isupper():         count_uppercase += 1         if characters1.islower():         count_lowercase += 1     is_password is good = True     if len(password) <= 7:         print "Passwords must be at least 8 characters long"     is_password is good = False     if count_uppercase < 1:         print "Your password must...

  • Please make this Python code execute properly. User needs to create a password with at least...

    Please make this Python code execute properly. User needs to create a password with at least one digit, one uppercase, one lowercase, one symbol (see code), and the password has to be atleast 8 characters long. try1me is the example I used, but I couldn't get the program to execute properly. PLEASE INDENT PROPERLY. def policy(password): digit = 0 upper = 0 lower = 0 symbol = 0 length = 0 for i in range(0, len(password)): if password[i].isdigit(): # checks...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • JAVA Q The criteria for a valid simple username is as follows: It should start with...

    JAVA Q The criteria for a valid simple username is as follows: It should start with an English alphabet followed by alphanumeric characters. No special characters allowed Username length should be at least 3 and should not exceed 20. You are required to complete the regular expression only. Your answer will be tested automatically. For example: the following are valid username: abc001 davidsmith And the following are invalid username: 12helloworld" 336.12.1.2 Test Result isUsername("abc001"); isUsername("128.1.1"); abc001: matches 128.1.1: does not...

  • Python Create a function that checks user input in an attempt to create a password. The...

    Python Create a function that checks user input in an attempt to create a password. The valid_password function accepts a password as an argument and returns either true or false to indicate whether the password is valid. A valid password must be at least 7 characters in length, have at least one uppercase letter, one lowercase letter, and one digit. Respond with output to the user as to whether the password is valid or not, and allow the user to...

  • Create a program via Java that has atleast 8 characters long, one upper case, and one...

    Create a program via Java that has atleast 8 characters long, one upper case, and one lower case, and one digit. here is my code:     public static void main(String[] args)     {         //variables defined         String passwords;         char password =0;                 //settings up password condition to false         boolean passwordcondition= false;         boolean size=false;         boolean digit=false;         boolean upper=false;         boolean lower=false;         //inputting a scanner into the system         Scanner keyboard = new Scanner(System.in);...

  • Keeping this code written as close to this as possible and only using these headers. How...

    Keeping this code written as close to this as possible and only using these headers. How do I turn this in to a code that can read from 3 files of passwords then sort them into two files. One for valid passwords and the other for invalid passwords and then send a message to the user that says how many are valid and how many are invalid. Only using std::ifstream fin; fin.open("data.txt"); fin.close(); std::ofstream fout; fout.open("data.txt"); fout.close(); #include <iostream> #include...

  • Good evening, I've been toiling around with restructuring a program which is supposed to register a...

    Good evening, I've been toiling around with restructuring a program which is supposed to register a preexisting input file, which contains four different passwords, and have it pass through the verification process, to see which of those passwords meets all the criteria set by the highlighted parameters. The code stipulates that in order for a password to be considered valid, it should contain a minimum of 6 characters in total, at least 2 lowercase letter, at least 1 uppercase letter,...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT