Problems checking input in java

The name is deceiving, but I really did not know how to set it.

I play with java. This is my code:

package zodiac;

import java.util.Scanner;

public class Zodiac {

   public static void main(String[] args) {
      Scanner username = new Scanner(System.in);
      String uname;

      System.out.println("Please enter your username: ");
      uname = username.nextLine();

      boolean test = (uname.length() <= 3);

      int trip = 0;
      while (trip == 0) {
         trip++;
         if (test) {
            trip = 0;
            System.out.println("Sorry username is too short try again");
            uname = username.next();
         } 
         else {
            System.out.println("Welcome Mr/Mrs: " + uname);
         }
      }
   }
}

what I'm trying to do is encourage the user to enter their username, and as soon as they check whether they are less than or less than 3, so that they enter the username again if the username, if more than 3 characters print the greeting mr / mrs blablabla

at the moment, if the username, if it contains more than 3 characters, displays a welcome message, however if you enter 3 or less characters, you will be prompted to enter the username again, and if you enter the username with more than three afterword characters, says the password is too short.

. java , , , .

+4
2

, :

  • , ,
  • do-while ( !

: ! :

public static void main(String[] args) {
    Scanner username = new Scanner(System.in);
    String uname;
    System.out.println("Please enter your username: ");

    boolean tooShort = true;
    do {
        uname = username.next();

        if (uname.length() <= 3)
            System.out.println("Sorry username is too short try again");
        else {
            System.out.println("Welcome Mr/Mrs: " + uname);
            tooShort = false;
        }

    } while (tooShort);

    username.close();
}
+2

boolean test = (uname.length() <= 3)

+1

Source: https://habr.com/ru/post/1617020/


All Articles