4

The title is deceiving however I didn't really know how to ask this.

I am playing around 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 prompt the user to enter their username and once they do check if the name is less than or equal to 3 make them type the username again, if the username if more than 3 chars print welcome mr/mrs blablabla

at the moment if the username if more than 3 chars it prints out the welcome message, however if you enter 3 or less chars it tells you to enter the username again and if you type a username with more than 3 chars afterwords it keeps telling the user that the password is too short.

How can i fix this. I have just recently started studying java at university however my teachers lack motivation to teach so i have to result to the internet, thank you.

5
  • That's because you don't update test. Commented Nov 23, 2015 at 8:34
  • That is not java specific - it is specific to the code flow you implemented (and would have the same effect in all other languages). At least, you need to move the test inside your while loop. Otherwise, test remains set to the very first check you did. Commented Nov 23, 2015 at 8:34
  • just include test = (uname.length() <= 3); after username.next();. Commented Nov 23, 2015 at 8:35
  • boolean test = (uname.length() <= 3); is computed only once - you need to recompute it each time new input is provided by user. Commented Nov 23, 2015 at 8:35
  • Thank you, due to lack of sleep and knowledge in java it's so easy to make such a stupid mistake :) Commented Nov 23, 2015 at 8:36

2 Answers 2

2

There are two things that you might want to think about in your code:

  • don't use an integer to stop looping if a boolean would be sufficient
  • a do-while loop might be more appropriate for your case (you don't have to rewrite code that way!

Now to your question: You are not checking the required minimum length of the inserted string in your loop again! This code might help you to understand all of the points i mentioned:

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

insert boolean test = (uname.length() <= 3) in your loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.