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.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
SebastianZdroana
  • 209
  • 1
  • 11
  • That's because you don't update `test`. – Maroun Nov 23 '15 at 08: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. – Andreas Fester Nov 23 '15 at 08:34
  • just include `test = (uname.length() <= 3);` after `username.next();`. – SomeJavaGuy Nov 23 '15 at 08:35
  • `boolean test = (uname.length() <= 3);` is computed only once - you need to recompute it each time new input is provided by user. – Łukasz Rogalski Nov 23 '15 at 08:35
  • Thank you, due to lack of sleep and knowledge in java it's so easy to make such a stupid mistake :) – SebastianZdroana Nov 23 '15 at 08:36

2 Answers2

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();
}
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
1

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

Si mo
  • 969
  • 9
  • 24