1

Basically, I have a text file with rows that correspond to an objects description (variables of the object). By this, I mean for instance, a row could look like: String int int double int long. Now, I have an empty array of those type of objects. My goal is to transfer the objects from the text file, to the array. I have searched for a solution though I have not found anything. Here is what I tried to do. I got a java.util.InputMismatchException though I do not know how to solve the issue. Thank you very much for your help.

     Scanner transfer = new Scanner (new FileInputStream(a));
    // we use a simple for loop to set every variable of the object to the file's order.
    for (int i = 0 ; i< Array.length; i++){
        Array[i].setLong(transfer.nextLong());
        Array[i].setString(transfer.next());
        Array[i].setInt(transfer.nextInt());
        Array[i].setString(transfer.next());
        Array[i].setDouble(transfer.nextDouble());
        Array[i].setInt(transfer.nextInt());
        }
    transfer.close();

Edit2 new stack trace happening at the first transfer

    Exception in thread "main" java.lang.NullPointerException
at Driver.main(Driver.java:31)
0

1 Answer 1

1

Just try putting in a couple if statements with else clauses that print whether or not the if worked...

    if(transfer.hasNextDouble()){
      Array[i].setDouble(transfer.nextDouble());
    }else{ 
    System.out.println("expecting a double, but got"+transfer.getNext());
    }

    if(transfer.hasNextLong()){
       Array[i].setLong(transfer.nextLong());
    }else{
    System.out.println("expecting a Long, but got"+transfer.getNext());
    }...

etc... that way you'll know whats going wrong

  Scanner transfer = new Scanner (new FileInputStream(a));
// we use a simple for loop to set every variable of the object to the file's order.
for (int i = 0 ; i< Array.length; i++){
    Scanner s2= new Scanner(transfer.nextLine())
    Array[i].setLong(s2.nextLong());
    Array[i].setString(s2.next());
    Array[i].setInt(s2.nextInt());
    Array[i].setString(s2.next());
    Array[i].setDouble(s2.nextDouble());
    Array[i].setInt(s2.nextInt());
    s2.close();
    }
transfer.close();

It's ugly, but it might be an issue with your original scanner not making it to the next line. Can't be sure without the file and code though.

Add the following to your code:

1) Verify that the scanner has as many lines as your array has entries

int count;
for(count=0; transfer.hasNextLine(); transfer.nextLine()){}
System.out.println(count==Array.length);

2) verify each line has 6 values //im assuming you can do this. just use string.split(delimeter) and count the length of the resulting array

3) Verify that each line has the types you specified

  for (int i = 0 ; i< Array.length; i++){
        Scanner s2= new Scanner(transfer.nextLine())
        boolean hasLong= s2.hasNextLong();
        //consume the input
        s2.next();
        \\next is a string so skip it
        s2.next();
        boolean hasInt= s2.hasNextInt();
        //consume the input
        s2.next();
        s2.next();
        boolean hasDouble=s2.hasNextDouble();
        s2.next();
        boolean hasInt2= s2.hasNextInt();
        //consume the input
        s2.next();
        boolean allTrue=hasLong&&hasInt&&hasDouble&&hasInt2;
        if(!allTrue){
        //print the line number so you can check where the issue is in the file
        System.out.println("error on line: "+i);
        //print the values of the booleans if you want
        }
        s2.close();
        }
    transfer.close();

this should be a good enough start

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

Comments

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.