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]);
        }
}
12
  • while(my_input.hasNext() && !names.equals("-1"))? Commented Sep 30, 2015 at 11:23
  • The last line of my document contains -1. So i want it to read the names until it encounters -1. Commented Sep 30, 2015 at 11:25
  • see names is an array. Commented Sep 30, 2015 at 11:26
  • Taking a look to this Commented Sep 30, 2015 at 11:26
  • Ah, I see. Is there a way I can fix that? Commented Sep 30, 2015 at 11:27

3 Answers 3

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

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));
    }
}

Comments

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].

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.