I have the following piece of code. I do not understand why its not working.

I'd really appreciate help on this.

import java.util.Scanner;
import java.io.*;

class ReadFiles {
	String [] codes = new String[99];
	int i = 0;

	private Scanner readCodes;

	public void openCodesFile() {
		try {
			readCodes = new Scanner(new File("C:/Users/Carlo/Desktop/Files/codes.txt"));
		} catch (Exception e) {
			System.out.println("Could not locate the data file!");
		}
	}

	public void readCodesFile() {

		while(readCodes.hasNext()) {
			codes[i] = readCodes.nextLine();
			i++;
			System.out.println(codes[i]);
		}
	}

	public void closeCodesFile() {
		readCodes.close();
	}
}

class NewHardware {
	public static void main(String[] args) {
		ReadFiles codesRead = new ReadFiles();
		codesRead.openCodesFile();
		codesRead.readCodesFile();
		codesRead.closeCodesFile();
	}
}

The output prints out "null" a bunch of times.

Also, I want to be able to not only print out the codes but use the codes array in the class NewHardware and manipulate it (print it out, truncate it, etc).

The text file contains the following:

G22
K13
S21
I30
H15
N23
L33
E19
U49

Solved it by fixing my readCodesFile() method like so:

public void readCodesFile() {

		while(readCodes.hasNext()) {
			codes[i] = readCodes.next();
			System.out.println(codes[i]);
			i++;
		}
        }
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.