I have recently been writing some code that asks the user questions about german.

br = new BufferedReader(new FileReader("C:\\Users\\Patrick\\Desktop\\Java Workspace\\germanTool\\src\\germanTool\\lesson1.txt"));
String[] engList = null;
String[] deuList = null;
int i = 0;
while (br.readLine() != null){
    lineCount++;
    line = br.readLine();
    engList[i] = line.split("\t")[0];
    deuList[i] = line.split("\t")[1];
}

Using this code I have been trying to read a file. On each line of the file there is a translation of an english word into german seperated by a tab. I want to write all of the english words into one array and all of the german ones into another array with the two counterparts having the same index in their arrays. How would I do this?

Recommended Answers

All 9 Replies

That code is mostly OK.
If you use i to index into the arrays to add the words (lines 8,9) then you need to increment it after processing each line (like lineCount).
Lines 2 and 3 create two variables that can refer to String arrays, but you haven't actually created any arrays. To create an array you need the new keyword, as in (eg)

String[] engList = new String[100]; // creates array with 100 elements

The problem is that you have to guess how big to make the array - which is why people tend to use classes like ArrayList rather than simple arrays.

Thanks for the reply. What exactly is ArrayList?

It's a class in the standard Java API that holds a list of values, like an array, but it grows automatically to fit whatever data you put in it. In this case you don't know in advance how many lines there will be in the file, so it's hard to guess how big your arrays need to be. With an ArrayList you don't need to worry about that.
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

br = new BufferedReader(new FileReader("C:\\Users\\Patrick\\Desktop\\Java Workspace\\germanTool\\src\\germanTool\\lesson1.txt"));
            while (br.readLine() != null){
                lineCount++;
                line = br.readLine();
                engList.add((line.split(";")[0]));
                deuList.add((line.split(";")[1]));

I've altered the code, as well as making the file itself use semi-colons for seperation. Now the code gets unhappy and gives me this:
java.lang.NullPointerException

You still need to create those lists - just declaring the variables isn't enough. (Maybe you just didnlt post that part of the code.)
Also
watch out for your readLines...
starting from the beginning of your loop...
line 2 reads the first line, and discards it
line 4 reads the second line (if there is one) and uses it
line 2 reads the third line, and discards it
line reads the fourth line (if there is one) and uses it
(etc)
ie every call to readLine() reads another line (until you reach EOF, when it reurns null - which may then cause a NullPointerException)

public static void main(String[] args) {
        int index = 0;
        int oldIndex;
        int language;
        int play = 1;
        int score;
        int lineCount = 0;
        ArrayList engList = new ArrayList();
        ArrayList deuList = new ArrayList();
        String answer;
        String line = "";
        Scanner sc = new Scanner(System.in);
        BufferedReader br;
        try{
            br = new BufferedReader(new FileReader("C:\\Users\\Patrick\\Desktop\\Java Workspace\\germanTool\\src\\germanTool\\lesson1.txt"));
            while (line != null){
                lineCount++;
                line = br.readLine();
                engList.add((line.split(";")[0]));
                deuList.add((line.split(";")[1]));
            }
        }catch(IOException e1){
            System.out.println("NO");
        }

Here is more of my code. I don't understand what I have to do to make it work.

The usual way to read a text file is to try to read the line and test for null/EOF at the same time, ie

String line;
while ((line=br.readLine()) != null) {
  // do something with line
}

anyway, I'm off to bed now. Nighty night. J

good night, thanks for all the help!

By now your probably snug in bed, but it did work and I've got it working, thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.