0

I am trying to read a txt file and trying to save it in an array. following is the format of the txt file:

A B 5
A C 2
A D 4
.....

public class search {
    public static void main(String[] args) throws ParseException {
        try {  

            Scanner user_input = new Scanner(System.in); // for user input            
            System.out.println("Enter the file name: ");
            String filename1 = user_input.next();

            File file = new File(filename1);

            search bd = new search();
            Node[] nodes;
            nodes = bd.getNodes(file);
            bd.printNodes(nodes);

        }
        catch(Exception e) {
            System.out.println("Error reading file " + e.getMessage());
        } 
    }    

    public Node[] getNodes(File file) throws IOException {
        FileReader bd = new FileReader(file);
        BufferedReader bufferReader = new BufferedReader(bd);
        String line;
        ArrayList<Node>list = new ArrayList<Node>(); 
        while ((line = bufferReader.readLine()) != null) { 
            String[] token = line.split(" "); // create string of tokens
            list.add(new Node(token[0], token[1], Integer.parseInt(token[2])));

        }
        bufferReader.close(); 

        return list.toArray(new Node[list.size()]); // converting list to array
    }

    public void printNodes(Node[] nodes) {
        System.out.println("======================");

        for(Node node : nodes) {
            System.out.println(node);
        }
        System.out.println("======================");
    }

Following is my Node class

class Node {

    String leftchild;
    String rightchild;
    int cost;

    public Node(){

    }

    public Node(String firstchild, String secondchild, int cost){
        this.leftchild = firstchild;
        this.rightchild = secondchild;
        this.cost = cost;
    }

    public Node(String firstchild, String secondchild) {
        this.leftchild = firstchild;
        this.rightchild = secondchild;
    }

    public ArrayList<String> getChildren(){
        ArrayList<String> childNodes = new ArrayList<String>();
        if(this.leftchild != null)
        {
            childNodes.add(leftchild);
        }
        if(this.rightchild != null) {
            childNodes.add(rightchild);
        }
        return childNodes;
    }

    public boolean removeChild(Node n){
        return false;
    }

    @Override
    public String toString() {
        return leftchild +" "+ rightchild;
    }

}

I have no compiling issues, but when I run my code, it is giving me error as

error reading file 1

Not sure why. I have tried to change my code in many ways but none of them worked. Could anyone please figure this out for me? Thanks

7
  • 1
    Use e.printStackTrace(); in the catch blocks to display the full error message. Commented May 18, 2016 at 17:53
  • Yes I have now used it and found the error to be Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 So I am guessing there is something problem with my printNodesmethod? Commented May 18, 2016 at 18:02
  • No need to guess when you can use Google. Commented May 18, 2016 at 18:03
  • I have googled it, but can't seem how to fix it :/ Commented May 18, 2016 at 18:10
  • The stacktrace gives you the linenumber of the exception, and if you really did google, you should know what an ArrayIndexOutOfBoundsException means. Commented May 18, 2016 at 18:15

1 Answer 1

1

If you get an ArrayIndexOutOfBoundsException: 1, it means that you have at least one line in your test input file that doesn't contain any spaces.

Indeed in your code you do String[] token = line.split(" ") which will extract all the tokens separated by a space and put them into an array of String, so if you get this error it means that the length of the array is 1 such that you cannot access to token[1] as it refers to the second element of your array that only contains one element.

So in your case you should test first if the length of the array has the right length as next:

String[] token = line.split(" "); // create string of tokens
if (token.length >= 3) {
    list.add(new Node(token[0], token[1], Integer.parseInt(token[2])));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Could you please be a bit specific?
Oh okay sorry let me check
response improved to give you a way to avoid it
Thank you so much! this worked. But however it is not printing my token[2] (cost). It is just giving me the first two tokens of strings. What could be the reason to it?
because it is not added in your toString method, try return leftchild + " " + rightchild + " " + cost;
|

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.