Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:17:34+00:00 2026-06-01T04:17:34+00:00

I’ve been working on this for a long time now and can’t figure out

  • 0

I’ve been working on this for a long time now and can’t figure out why this wont serialize my data. The game is meant to store the user-typed question and answer when it gets it so the next time the game is opened it will have those questions asked… for example does the animal fly … no … input a question that will help me guess next time … it doesn’t fly.. what is it ? monkey…. ok so next time the game runs … it will ask if it doesn’t fly and if it is a monkey. but the problem is i cant seem to put this together.

If i could get help quickly – i would really appreciate it. Thanks.

//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

}

  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:17:35+00:00Added an answer on June 1, 2026 at 4:17 am

    You are calling play again on animal, not the instance that you have tried to deserialize.

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.