3

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

I was thinking of doing the following with readCodesFile():

public String readCodesFile() {

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

Or something but it hasn't worked just yet. Am I on the right track?

Oh, just wanted to add that the text contains the following:

G22
K13
S21
I30
H15
N23
L33
E19
U49

EDIT:

Thanks to Tony and Churk below to help me with my idiocy. I am accepting Tony's answer basically because he challenged me to think but Churk's answer is just as valuable.

For the second part of my question (where I asked about being able to use it in class NewHardware), I did the following:

class NewHardware {
    public static void main(String[] args) {
        ReadFiles codesRead = new ReadFiles();

        codesRead.openCodesFile();

        codesRead.readCodesFile();

        for (int i = 0; i < 9; i++) {
            System.out.println("\n\n" + codesRead.codes[i]);
        }

        codesRead.closeCodesFile();
    }
}

This is of course not the final program code but this has helped me get the basic idea. Hope this helps others too.

1
  • 1
    +1 for doing the work and asking a coherent question. Commented Feb 26, 2012 at 5:57

2 Answers 2

2
codes[i] = readCodes.nextLine();
            i++;
            System.out.println(codes[i]);

You are printing codes[i++]

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

1 Comment

Thanks for the answer, I figured it out because of Tony below and then read your answer and comment.
1

Look carefully at your readCodesFile() method. Look at every line. What is it doing? Can you explain it to us?

2 Comments

Well, I was hoping it would be adding the codes from the text file to the array. I am new to this, never done it, so I can't really understand what I am doing wrong. I know there is something wrong but just don't know what it is. Hence I posted it here to get some sort of direction.
Ok, " codes[i] = readCodes.nextLine();" reads a line and jams it into codes[i]. Then "i++" increments the index. What happens now with the println??

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.