title: How to check if a username exists in a text file via File/FileWriter/Scanner classes
body: <p>I'm not sure what I'm doing wrong because after running the program in my main class, it completely skips over the </p>
  
  <pre><code>while(usernameCheck.hasNext()){
      //code...
  }
  </code></pre>
  
  <p>loop even though I purposely put a username that's already in my "accounts.txt" file. I also tried the <code>contains()</code> method but obviously if I put lets say "John1" and "John" is already in my accounts.txt file, it won't accept it</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  public class CreateAccount {
  
  public static void newAccount() throws IOException{
  
      Scanner keyboard = new Scanner(System.in);
      FileWriter fw = new FileWriter("accounts.txt",true);
      PrintWriter pw = new PrintWriter(fw);
  
  
      System.out.println("Please enter a username: ");
      String user = keyboard.nextLine();
  
      File file = new File("accounts.txt");
      Scanner usernameCheck = new Scanner(file);
  
      while(usernameCheck.hasNext()){
          String existingUsername = usernameCheck.nextLine();
  
          if(user.equalsIgnoreCase(existingUsername)){
  
              System.out.println("Username already exists");
              System.out.println("Please enter a username: ");
              user = keyboard.nextLine();
          }
      }
  
      System.out.println("Please enter a password: ");
      String pass = keyboard.nextLine();
  
      System.out.println("Account created!");
  </code></pre>
  
  <p>*** After reading my code I realize I also have another problem: Even if I do create a username that doesn't exist in my "accounts.txt" class, it'll keep repeating the prompt that a username already exists because of the hasNext() method. What can I do about this?</p>