0

I'm a newbie in java from c# background. In c# when i want to make sure the user cannot does null data in the Console Application i make a loop like

static void Main(string[] args)
{
    Console.WriteLine("Enter your name : ");
    string name = Console.ReadLine();

    while (name == "")
    {
        Console.WriteLine("Enter your name : ");
        name = Console.ReadLine();
    }
}

Now I want to implement the same in java. I am using

public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Enter your name : ");
    String pname;
    Scanner scan=new Scanner(System.in);
    pname=scan.next();

    while ("".equals(pname))
    {
        System.out.println("Enter your name : ");
        pname=scan.next();
    }
}

But when a null value is entered, the output doesn't show the Enter your name again it only moves one line waiting for a value to be entered.

What am i doing wrong?

4
  • How do you enter a null value? Commented Oct 2, 2013 at 21:44
  • @Joni By just hitting the enter key without typing anything. Thats how its done in c#. I am making a mistake? Commented Oct 2, 2013 at 21:45
  • "While loop doesn't work well in java", hilarious title, isn't it? I wonder how can you think that the language is not working well instead of you since you even admit to be a newbie. Commented Oct 2, 2013 at 21:46
  • @Jack Sorry pls can u help me edit. just didn't know what to use Commented Oct 2, 2013 at 21:47

4 Answers 4

1

Try using nextLine() instead. next() only gets up to the next space, nextLine() gets the next linebreak. I vaguely remember this from my java class in college.

Sign up to request clarification or add additional context in comments.

4 Comments

Did my answer offend you..?
Usually when people have several ..... at the end, it implies sarcasm. (I'm glad it wasn't in this case lol)
Lol i never knew that
I bet it's a culture thing. Anyways, glad I could help, good luck with your project.
0
   while ("".equals(pname))
        {
           System.out.println("Enter your name : ");
           pname=scan.next();
        }

Instead use

     while (pname.equals(""))
     {
        System.out.println("Enter your name : ");
        pname=scan.next();
     }

If you look at javadocs for equals, you will know what i am talking about. And also a hint, i started using buffred reader when i started coding in java

2 Comments

This won't make a difference because equals is symmetric. Actually the original is preferred if there's a chance pname is actually null.
This is not correct. If pname is NULL, in the second form you obtain a NullPointerException ;-)
0

You can use:

while (pname == null || "".equals(pname))
{
    System.out.println("Enter your name : ");
    pname = scanner.nextLine();
}

Comments

0

I also faced similar type of problem once. The problem is not in while loop. It is in reusing same Scanner object. Try creating new Scanner object each time.

public static void main(String[] args) {
// TODO code application logic here
System.out.println("Enter your name : ");
String pname;
Scanner scan=new Scanner(System.in);
pname=scan.next();

while (pname.equals(""))
{
    System.out.println("Enter your name : ");
    scan = new Scanner(System.in);
    pname=scan.next();
}

}

Comments

Your Answer

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