2012-04-02 31 views
0

我一直在研究這個很長一段時間,不知道爲什麼這不會序列化我的數據。遊戲是爲了存儲用戶輸入的問題和答案,當它獲得它,所以下次打開遊戲時,會問這些問題...例如動物飛行...不...輸入一個問題這將幫助我猜下一次......它不會飛......它是什麼?猴子....好吧,所以下次遊戲運行時...它會問它是否不飛,如果它是一隻猴子。但問題是我似乎不能把它放在一起。GuessAnAnimal不會序列化

如果我能很快得到幫助 - 我會很感激。謝謝。

//GuessTheAnimal Class----------------------------------- 
import javax.swing.JOptionPane; 

import java.io.FileInputStream; 
import java.io.ObjectInputStream; 
import java.io.Serializable; 
import java.io.ObjectOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 


public class GuessTheAnimal implements Serializable 
{ 


public static void main(String[] args) 
{ 
Tree animal = new Tree(); 
animal.instruction(); 
animal.play(); 
animal.writeToFile("treeData.ser"); 
Tree newAnimal = Tree.readFromFile("treeData.ser"); 
animal.play(); 



} 

} 

//Tree Class----------------------------------------- 
    import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 

import javax.swing.JOptionPane; 


    public class Tree 
//Tree class 
{ 
private Node root; 

//constructor 
public Tree() 
{ root = new Node(); 
    root.leftChild = new Node(); 
    root.rightChild = new Node(); 
    root.questionText = "Does it live on land?"; 
    root.leftChild.questionText ="bear"; // left side is Yes, right side is No 
    root.rightChild.questionText = "parrot"; 
} 

public void instruction() 
{ 
    JOptionPane.showMessageDialog(null, "Think of an animal, I will try to guess it, answer yes or no"); 
} 


public void play() 
{ 
Node current = root; 
Node parent = current; 
boolean isLeftChild = true; 


while(true) 
{ parent = current; 
    int response = JOptionPane.showConfirmDialog(null,current.questionText); 
    //code here for yes 
    if (response == JOptionPane.YES_OPTION) 
    { 
     current = current.leftChild; 
     isLeftChild=true; 
    } 
    //code here for no 
    else if (response == JOptionPane.NO_OPTION) 
    { 
     current = current.rightChild; 
     isLeftChild = false; 
    } 


    if (current.leftChild == null && current.rightChild == null) 
    { 
     int secondQ = JOptionPane.showConfirmDialog(null, "Is your animal a " + current.questionText + "?"); 

     if (secondQ == JOptionPane.YES_OPTION) 
     { 
      JOptionPane.showMessageDialog(null,"I Guessed your animal!"); 
      return; 
     } 
     else if (secondQ == JOptionPane.NO_OPTION) 
     { 
      Node nodeOne = new Node(); 
      Node nodeTwo = new Node(); 

       nodeOne.questionText = JOptionPane.showInputDialog("Write a question that differentiates your animal from the animal I guessed, it would be yes for your animal"); 

       nodeTwo.questionText = JOptionPane.showInputDialog("What is this animal?"); 

       nodeOne.rightChild = current; 
       nodeOne.leftChild = nodeTwo; 

       // parent.leftChild = nodeOne or parent.rightChild = nodeOne 
       if(isLeftChild == false) 
       { 
        parent.rightChild = nodeOne; 
        System.out.println("right child"); 
       } 
       else 
        { 
        parent.leftChild = nodeOne; 
        System.out.println("left Child"); 
        } 
       return; 




     } 


    } 

} 

} 

public void preOrder(Node localRoot) 
{ 
if(localRoot != null) 
    { 
    System.out.print(localRoot.questionText + " "); 
    preOrder(localRoot.leftChild); 
    preOrder(localRoot.rightChild); 
    } 
} 

public Node getRoot(){ 
    return root; 
} 
public boolean writeToFile(String text) { 
    try { 
     FileOutputStream fileOut = new FileOutputStream("treeData.ser"); 
     ObjectOutputStream out = new ObjectOutputStream(fileOut); 
     out.writeObject(root); 
     out.close(); 
     fileOut.close(); 
     return true; 
} catch (java.io.IOException e) { 
    e.printStackTrace(); 
    System.out.println("-----problem in writeToFile()--------"); 
    return false; 
} 
} 

/** @return tree read from file, if successful input, else null */ 
public static Tree readFromFile(String fileName) { 
    try { 
    FileInputStream fileIn = new FileInputStream("treeData.ser"); 
    ObjectInputStream objIn = new ObjectInputStream(fileIn); 
    Tree newAnimal = (Tree) objIn.readObject(); 
    objIn.close(); 
    fileIn.close(); 
    return newAnimal; 
    } 
    catch (Exception e){//Catch exception if any 
     e.printStackTrace(); 
     System.out.println("-----problem in readFromFile()--------"); 
    } 
    return null; 
} 
} 


//Node Class---------------------------------- 


public class Node { 
//Node class 
//instance variables 
public String questionText; 
public Node leftChild; 
public Node rightChild; 

public void displayText() 
{ 
    System.out.println(questionText); 
} 


/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

} 

    } 

回答

0

您在animal上再次呼叫播放,而不是您試圖反序列化的實例。

Tree animal = new Tree(); 
animal.instruction(); 
animal.play(); 
animal.writeToFile("treeData.ser"); 
Tree newAnimal = Tree.readFromFile("treeData.ser"); 
///should the following not be newAnimal? 
animal.play(); 
2

如果您要序列化一個對象,則不僅必須將對象序列化,而且它的所有子對象也必須是可序列化的。如果你要序列化你的樹,你必須將樹標記爲可序列化。樹的組件也必須是可序列化的類,等等,所以你需要使你的Nodes可序列化。我沒有看到Node類的聲明,但是這也適用於它所持有的任何成員變量。

+0

好的,所以我是一種初學者 - 但你說要在我所有的類中實現可序列化,然後我應該設置? – Ignacious 2012-04-02 03:48:58

+0

實際上我確實修復了一下,它似乎在寫,但它不會讀回(崩潰)說java.lang.ClassCastException:節點不能被轉換爲樹 \t在Tree.readFromFile(Tree.java:134 ) \t at GuessTheAnimal.main(GuessTheAnimal.java:27) – Ignacious 2012-04-02 03:51:35

相關問題