I have just started learning Java and I am trying to read names from a text file that I created. Then I want my program to ask the user for a name and then check if the name is in that list. However, I am having trouble working with arrays so first I am trying to only read the names and then store them in an array. Here is what I have done so far.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class readnames
{
public static void main(String[] args) throws FileNotFoundException
{
File file=new File("names.txt");
Scanner my_input = new Scanner(file);
int i=0;
String[] names = new String[20] ;
while(my_input.hasNext() && !names.equals("-1"))
{
names[i]=my_input.nextLine();
i++;
}
my_input.close();
System.out.println(names[i]);
}
}