0

I'm a beginner on java and I'm having problem using binary file input and output of an array list. I'm trying to store the data on the array list on a file and then use it to display it in the console. Here is some of my code, it runs but displays the wrong information and I'm also getting a warning. Any help on what is causing this problem? Thanks!

public class Towers {
    public static ArrayList<String> allMoves= new ArrayList<String>();
    static{
        allMoves.add( "These Are the Disk Moves:" );
    }

    public static void move(final int aNumDisks){
        move(aNumDisks, 1, 2, 3);
        String fileName = "solution.dat";
        try{
            ObjectOutputStream outputStream = 
                    new ObjectOutputStream(
                            new FileOutputStream (fileName));
            outputStream.writeObject(allMoves);
            outputStream.close( );
        }
        catch ( IOException e ){
            System.out.println("Error writing to file " + fileName);
            System.exit(0);
        }
    }

Display output file in the console.

public class TReporter {        
    public static void reportSol(){
        String fileName = "solution.dat";
        ArrayList<String> allMovesA = null;
        try{
            ObjectInputStream inputStream =
                    new ObjectInputStream(
                            new FileInputStream (fileName));
            allMovesA = (ArrayList<String>)inputStream.readObject(); //WARNING!
                           //Type safety: Unchecked cast from Object to ArrayList<String>
            inputStream.close();
        }
        catch (Exception e){
            System.out.println("Problem reading the file " + fileName);
            System.exit(0);
        }
        for (int i = 0; i < allMovesA.size(); ++i){
            System.out.println( allMovesA.get(i) );
        }

It should display something like this:

=== 0 disks move(int) ===
These Are the Disk Moves:

=== 0 disks move(,,) ===
These Are the Disk Moves:

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 3 to 2
Move disk from 3 to 1
Move disk from 2 to 1

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

But im getting this:

=== 0 disks move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 0 disks move(,,) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3
JProg
  • 189
  • 1
  • 9
  • 15
  • 2
    What are you expecting to be displayed? What are you currently seeing displayed? What warning are you getting? These are things that you need to post in order for people to understand why your program doesn't work. – Hunter McMillen Apr 24 '12 at 16:51
  • 1
    Is it homework? If yes, tag it as such. I don't see any main method nor any move(int, int, int, int). Please post them as well – Guillaume Polet Apr 24 '12 at 16:53

1 Answers1

2

The warning you get is completely normal. You can suppress it by inserting this line before the line with the warning:

@SuppressWarnings("unchecked")

And for the wrong info you got:

  • make sure you write the file before you try to read it, you might be using an old version of the file.
  • make sure that the ArrayList it does contain the right moves.
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287