1

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]);
        }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
madlin
  • 113
  • 2

3 Answers3

0
    public static void main(String[] args) throws FileNotFoundException {
            File file = new File("names.txt");
            Scanner my_input = new Scanner(file);

            ArrayList<String> names = new ArrayList<>();

            while (my_input.hasNext()) {
                 String t =my_input.nextLine();
                 if( !t.equals("-1"))
                 names.add(t);

            }


            System.out.println("Enter name");
            String nameToCheck = my_input.nextLine();
            if (names.contains(nameToCheck)) {
                System.out.println("Found");
            }
            my_input.close();
        }

I'd suggest you to use ArrayList<> because file may contain more than 20 names

With normal Arrays

public static void main(String[] args) throws FileNotFoundException {
        File file = new File("names.txt");
        Scanner my_input = new Scanner(file);

        String[] names = new String[20];
        int i = 0;
        while (my_input.hasNext()) {
             String t =  my_input.nextLine();
             if(!t.equals("-1"))
             names[i++] = t;
        }


        System.out.println("Enter name");
        String nameToCheck = my_input.nextLine();
        for (String temp : names) {
            if (temp.equals(nameToCheck)) {
                System.out.println("FOund");
            }
        }
        my_input.close();
    }
Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
0

!names.equals("-1") is always true, as names is an array. While not using an ArrayList (a dynamic sized array), here is a quick fix:

public class ReadNames {
    public static void main(String[] args) throws Throwable {
        File file = new File("names.txt");
        Scanner my_input = new Scanner(file);
        int i = 0;
        // FIXME the file you're reading may exceed 20 lines, use ``ArrayList`` instead
        String[] names = new String[20]; 
        while(my_input.hasNext()) {
            String line = my_input.nextLine();
            if(line.equals("-1")) {
                // end reading prematurely
                break;
            }
            names[i] = line;
            i++;
        }
        my_input.close();
        // print entire array
        System.out.println(Arrays.toString(names));
    }
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
0

You are incrementing the i variable inside the loop. If you can show the last name, you could decrement the variable before to name[i].

kiuby_88
  • 334
  • 1
  • 6
  • 18