0

I am having a problem with my while loop. It is compiling but it isn't entering while loop and it skips to next condition.

public class Tr {
    public static void main(String[] args)  {

        Scanner in = new Scanner(System.in);
        String name = "";

        while(name.length() > 1) {     
            System.out.print("Enter  name : ");
            name = in.nextLine( );  
            if(name.length() > 1) {
                System.out.println("It needs to be greater than  1");
            }
        }
    }
}
2
  • u can use while(name.length() == 0) { System.out.print("Enter name : "); name = in.nextLine( ); if(name.length() > 1) { System.out.println("It needs to be greater than 1"); } } Commented Oct 19, 2017 at 8:27
  • If i dont enter it it will print me error: variable name might not have been initialized Commented Oct 19, 2017 at 8:27

5 Answers 5

2

That's because the name has 0 length and hence, the control never enters while. You need to use do..while loop so that it executes at least once, e.g.:

do{     
   System.out.print("Enter  name : ");
   name = in.nextLine( );  
   if(name.length() <= 1){
        System.out.println("It needs to be greater than  1");
   }
}while(name.length() <= 1);
Sign up to request clarification or add additional context in comments.

2 Comments

Probably need to make those > into < but using a do ... while is the right answer.
@OldCurmudgeon updated the answer, thanks for your inputs
2

It appears that the logic you want is to prompt the user for a name, and keep prompting until a name with length greater than one is entered. A do loop would seem to fit well here:

Scanner in = new Scanner(System.in);
String name;

do {   
    System.out.print("Enter name with length > 1: ");
    name = in.nextLine();
    // you can print an optional feedback message
    if (name.length() <= 1) {
        System.out.println("length needs to be greater than 1");
    }
} while (name.length() <= 1);

1 Comment

No it still skips it if you press enter. I need to find if the user enters name smaller than 1 it will send him error message and he will need to input name again.
0

Your variable name gets initialized to name = "";, so name.length() == 0;.

Your while condition checks whether the length greater than 1, and it isn't so it skips.

2 Comments

yes but i need to initialize it or it will not work. Sry i am new to programing so i dont know alot.
I know you do, but your while loop is practically the same as while(false). Change the > in your while and if condition to <.
0

Because name.length() always returns 0 so your condition won't ever be true.

Comments

0

(name.length() < 1) change this condition in our while and if conditions.

You have defined an empty string called name and the condition for your while loop condition checks if the length of name is greater than 1 or not.

1 Comment

yes but i need to initialize it or it will not work. Sry i am new to programing so i dont know alot. I just need to user not to input name smaler than 1

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.