0

I'm trying to get saved data in a text file to an array to use it in my code and then search this array for a string submitted from the user from the GUI , but for some reason I print out the data in the array it is all null. here's the code !!

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class IO {

    File f = new File("DB.txt");
    PrintWriter write;
    Scanner input;
    String[][] data;
    String nameToSearch;

    // search constructor
    public IO(String name) {
        super();
        nameToSearch = name;
        try {
            input = new Scanner(f);
        } catch (FileNotFoundException e) {
            System.out.println("File not found please restart the program");
        }
        data = new String[linesCounter()][2];
        int i = 0;
        while (input.hasNext()) {
            data[i][0] = input.nextLine();
            data[i][1] = input.nextLine();
            i++;
        }
    }

    public IO(String name, String number) {
        try {
            write = new PrintWriter(new FileWriter(f, true));
        } catch (IOException e) {
            System.out.println("Error");
        }
        write.println(name);
        write.println(number);
        write.close();
    }

    int linesCounter() {
        try {
            input = new Scanner(f);
        } catch (FileNotFoundException e) {
            System.out.println("File not found please restart the program");
        }
        int counter = 0;
        while (input.hasNext()) {
            input.nextLine();
            counter++;
        }
        return counter / 2;
    }

    int contactFinder() {
        int i = 0;
        while (input.hasNext()) {
            if (data[i][0].equalsIgnoreCase(nameToSearch))
                return i;
            i++;
        }
        return -1;
    }

    String nameGetter() {
        return data[contactFinder()][0];
    }

    String numGetter() {
        return data[contactFinder()][1];
    }

}

2 Answers 2

1

It looks like you read all the lines in from the file to count how many lines there are, and then when you go to read the data, you're starting from where you left off, which would be the end of the file.

It's also worth noting that you can use commons-io FileUtils to easily read all the lines from a file.

For example:

List<String> lines = FileUtils.readLines(f);
String[][] data = new String[lines.length][2];
for (int i = 0; i < lines.size(); i++) {
    data[i][i % 2] = lines.get(i);
}

If you also don't want to use a (very useful) third party library, you could load up the data pretty simply with:

List<String> lines = new ArrayList<String>();
Scanner input = new Scanner(f);
while (input.hasNextLine()) {
    lines.add(input.nextLine());
}
input.close();

Then go into the array population.

Sign up to request clarification or add additional context in comments.

10 Comments

I thought that might be it but I could't reset the scanner back to start of the file, any idea how to do that ?
You could just create a new one, but there's no sense in reading the whole file twice. Just load it into a List<String> the first time around, preferably using a common utility like I added to my answer, and then go from there. Remember that a Scanner is basically a stream and needs to be closed.
I'm not familiar lists yet so I'm using a normal array instead
Lists are easier than arrays, actually. I added an example of loading up your data to my answer. I think that'll give you the array you're looking for in much less code.
is FileUtils a standard java class ?? because it cannot be resolved on Java !!
|
0

I would advice you to use RandomAccessFile. This has methods such as readLine() to read the line and seek(long pos) to set the file read pointer. You may use seek(0L) to restart the reading of the file.

2 Comments

what does the String mode means ??
@user1712638 Where do you see String mode?

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.