0
System.out.print("Enter email: ");
    String mailId = sc.next();
    boolean validMail = true;
    while(x.hasNext() && validMail == true)
    {
        String a = x.next();
        if(mailId.equals(a))
        {
            System.out.println("The mail id already exists!");
            System.out.print("Enter email again: ");
            mailId = sc.next();

            String b = x.next();
            if(!mailId.equals(b))
            {
                validMail = true;

            }
            String c = x.next();
            if(mailId.equals(c))
            {
                validMail = false;
            }

        }
    }

In this block of code i want the user to write a mail id and if the mail already exists in the text file it should ask the user to input a valid mail id. But in this case the program checks the validation only once and if an existing mail is printed twice then the program accepts it! Please help me on this...just suggests me a method where as i can ask the user a mail id and check if it is present in my text file if yes then ask one more until it is valid or else just let the program continue....

  • Does this answer your question? https://stackoverflow.com/questions/10604125/how-can-i-clear-the-scanner-buffer-in-java – Pluto May 21 '20 at 13:06
  • no... just suggests me a method where as i can ask the user a mail id and check if it is present in my text file if yes then ask one more until it is valid or else just let the program continue.. –  May 21 '20 at 13:14
  • 1
    Each time you want to start reading a textfile again from the beginning using a `Scanner`, you need to close the existing `Scanner` (e.g., `x.close()` and open the same file with a new `Scanner` (e.g., `x=new Scanner(new File("MyTextFile.txt"));`) – Kevin Anderson May 21 '20 at 13:35
  • Can u plz show it in my code...I beg –  May 21 '20 at 14:17

0 Answers0