Java system input password

Write a java program to Validating Input Password.

Write a code get a password as string input and validate using the rules specified below. Apply following validations:

  1. Minimum length should be 8 characters.
  2. Must contain any one of these three special characters @ or _ or #.
  3. May contain numbers or alphabets.
  4. Should not start with a special character or number.
  5. Should not end with a special character.

Input and Output Format

  • Input consists of a string.
  • Output is a string Valid or Invalid.

Refer sample output for formatting specifications

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:

Validating Input Password Program in Java

Following are the steps to validate passwords in Java:

  • First, input the password from the user.
  • Now, check for the following conditions:
    • First, the length should be greater than 8.
    • Next, check if the input string contains a special character.
    • If the first character is alphabet or not. if it is, then extract the character from the last position of the input string.
    • Then, check if that character is an alphabet or digits.
    import java.util.Scanner; public class Main < public static void main(String[] args) < Scanner sc = new Scanner(System.in); String a = sc.next(); int d = 0; if (a.length() >= 8) < if (a.contains("#") || a.contains("@") || a.contains("_")) < char c = a.charAt(0); //System.out.println(c); if (Character.isAlphabetic(c)) < char dd = a.charAt(a.length() - 1); if ((Character.isAlphabetic(dd)) || (Character.isDigit(dd))) < if (a.matches(".*3.*") || a.matches(".*[a-zA-Z].*")) < System.out.println("Valid"); >else < System.out.println("Not Valid"); >> else < System.out.println("Not Valid"); >> else < System.out.println("Not Valid"); >> else < System.out.println("Not Valid"); >> else < System.out.println("Not Valid"); >> >

    Output

    Write a java program to validate the Id

    Write a program to get two string inputs and validate the ID as per the specified format. The return type of the output is a string Valid Id or Invalid Id.

    Input and Output Format

    • Input consists of two strings.
    • The first string is ID and the second string is located. ID is in the format CBJ-LLL-XXXX where LLL is the first three letters of a given location and XXXX is a four-digit number.
    • The output is a string Valid id or Invalid id.

    Refer sample output for formatting specifications

    Sample Input 1:

    Sample Output 1:

    Sample Input 2:

    Sample Output 2:

    Program for Id validation in java

    Following are the steps to validate ID against a given format:

    • Input Id and location from the user.
    • Pass both of them to formattingString() method.
    • Inside the method, get the substring from the location and store it in the s3 variable.
    • use a StringTokenizer class to break a string into tokens by passing the ‘-‘ as a delimiter. get each token and store it in variables s4,s5, and s6.
    • Now, check for each token whether the following condition is matched:
      • First, check if the first token equals “CBJ” also if the value in s5 equals that in s3.
      • Then, check if the token in the s6 variable contains 4 digit number.
      import java.util.*; public class Main < public static void main(String[] args) < Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); String s2 = sc.nextLine(); boolean b = formattingString(s1, s2); if (b == true) System.out.println("Valid"); else System.out.println("Invalid"); >public static boolean formattingString(String s1, String s2) < String s3 = s2.substring(0, 3); boolean b = false; StringTokenizer t = new StringTokenizer(s1, "-"); String s4 = t.nextToken(); String s5 = t.nextToken(); String s6 = t.nextToken(); if (s4.equals("CBJ") && s5.equals(s3) && s6.matches("4")) b = true; else < b = false; >return b; > >

      Output

      Thus, in this way, we can validate passwords and ID in Java.

      Additionally, Check These java program Articles Too:

      Источник

      How to Read User Input and Password in Java from command line? Console Example

      Hello guys, if you are wondering how to take user input from command prompt in Java like username and password then don’t worry. Java provides several utilities like Scanner and BufferedReader to read input from command prompt in Java. Java 6 added a new utility class for reading input data from character based devices including command line. java.io.Console can be used to read input from command line, but unfortunately, it doesn’t work on most of the IDE like Eclipse and Netbeans. As per Javadoc call to System.Console() will return attached console to JVM if it has been started interactive command prompt or it will return null if JVM has been started using a background process or scheduler job.

      Anyway, java.io.Console not only provides a way to read input from command prompt or Console but also reading passwords from the console without echoing it.

      Console.readPassword() method reads password and returns a character array and password is masked during entering so that any peeping tom can not see your password while you are entering it.

      Here is a code example of How to read password and input from command prompt or console using java.io.Console . By the way apart from Console, you can also use Scanner or BufferedReader to read input from command prompt, as shown in this example.

      Java Program to read password from command prompt using Console class

      Here is the Java program which will demonstrate how you can use Console class to read input and password from command line. It’s much easier than Scanner or BufferedReader which was alternative solution.

      How to read password from command prompt in Java

      import java.io.Console; import java.io.IOException; public class ConsoleExampleJava < public static void main(String args[]) throws IOException < Console console = System.console(); System.out.println("Reading input from console using Console in Java6 "); System.out.println("Please enter your input: "); String input = console.readLine(); System.out.println("User Input from console: " + input); System.out.println("Reading password from Console in Java: "); //password will not be echoed to console and stored in char array char[] password = console.readPassword(); System.out.println("Password entered by user: " + new String(password)); > > Output: Reading input from console using Console in Java6 Exception in thread "main" java.lang.NullPointerException Please enter your input: at ConsoleExampleJava.main(ConsoleExampleJava.java:21)

      It’s always good to check whether System.console() returns console or null before using it. I haven’t used it here just to demonstrate this NullPointerException. Anyway I am still looking to make it work on NetBeans, IntelliJIDEA or Eclipse and will update you guys once I found a way. let me know if you guys know any way to do it.

      How to Read User Input and Password in Java from command line? Console Example

      You can see that when we entered password its not visible in the screen because we used Console’s password feature. If you have used Scanner then it would have been visible because Scanner doesn’t differentiate between password or any other text.

      You may also be wondering what is the first error we got, that’s actually because the filename and name of public class on Java code was different. Once I corrected that error was gone.

      That’s all on how to read input from the command line in Java using the Console class. As I said there are multiple ways to read input from the console and I recommend using Scanner for this task. Though there is nothing wrong with the BufferedReader approach as well Scanner is simply more convenient and more powerful.

      Источник

      In Java how to use System.console() to read user input text and password?

      In Java how to System.console() to read user input text and password?

      The password read using System.console().readPassword() can be used for various purposes, such as authentication or encryption.

      For example, you can use the password to check if it matches a stored password in a database or a file. If the passwords match, the user is authenticated. You can also use the password to encrypt sensitive information before storing it in a file or a database.

      It is important to keep in mind that the password entered using readPassword() is stored in a char[] array, which is more secure than a String object because a char[] array can be cleared from memory after use, while a String object cannot be cleared from memory easily.

      Here is an example of how you can use System .console() to read both user input text and password:

      package crunchify.com.java.tutorials; import java.io.Console; /** * @author Crunchify.com * Version: 1.1 * In Java how to System.console() to read user input text and password?? * */ public class CrunchifySystemConsoleReadData < public static void main(String[] args) < // Returns the unique Console object associated with the current Java virtual machine, if any. Console console = System.console(); if (console == null) < System.out.println("No console available"); // exit(): Terminates the currently running Java Virtual Machine. // The argument serves as a status code; by convention, // a nonzero status code indicates abnormal termination. System.exit(0); >// Read user input text console.writer().println("Enter your name: "); // readLine(): Reads a single line of text from the console. String name = console.readLine(); console.writer().println("Hello, " + name); // Read password console.writer().println("Enter your password: "); // readPassword(): Reads a password or passphrase from the console with echoing disabled. char[] password = console.readPassword(); console.writer().println("Password entered: " + new String(password)); > >

      Explanation:

      • The System.console() method returns a java.io.Console object that can be used to read input from the console.
      • The if statement is used to check if the console is available, as System.console() may return null in some cases (for example, when the application is not run from the command line).
      • The readLine() method is used to read user input text, and the readPassword() method is used to read a password.
      • The password entered by the user is not displayed on the console, but it is stored in a char[] array. To display it, we convert it to a String object.

      Let me know if you have any question running this code.

      If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

      Источник

      Java: Input and display your password

      Write a Java program to input and display your password.

      Pictorial Presentation:

      Java: Input and display your password

      Sample Solution:

      import java.io.Console; public class Example42 < public static void main(String[] args) < Console cons; if ((cons = System.console()) != null) < char[] pass_ward = null; try < pass_ward = cons.readPassword("Input your Password:"); System.out.println("Your password was: " + new String(pass_ward)); >finally < if (pass_ward != null) < java.util.Arrays.fill(pass_ward, ' '); >> > else < throw new RuntimeException("Can't get password. No console"); >> > 

      Flowchart: Java exercises: Input and display your password

      Java Code Editor:

      Contribute your code and comments through Disqus.

      What is the difficulty level of this exercise?

      Test your Programming skills with w3resource’s quiz.

      Follow us on Facebook and Twitter for latest update.

      Java: Tips of the Day

      Java Date vs Calendar

      Date is a simpler class and is mainly there for backward compatibility reasons. If you need to set particular dates or do date arithmetic, use a Calendar. Calendars also handle localization. The previous date manipulation functions of Date have since been deprecated.

      Both Date and Calendar are mutable, which tends to present issues when using either in an API.

      • Weekly Trends
      • Java Basic Programming Exercises
      • SQL Subqueries
      • Adventureworks Database Exercises
      • C# Sharp Basic Exercises
      • SQL COUNT() with distinct
      • JavaScript String Exercises
      • JavaScript HTML Form Validation
      • Java Collection Exercises
      • SQL COUNT() function
      • SQL Inner Join
      • JavaScript functions Exercises
      • Python Tutorial
      • Python Array Exercises
      • SQL Cross Join
      • C# Sharp Array Exercises

      We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

      Источник

      Читайте также:  Python find all spaces in string
Оцените статью