• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not getting proper Output  RSS feed

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm at present learning serialization & i'm have encountered an problem with the following code

<CODE>

import java.io.*;

class GameCharacter implements Serializable
{

int num;
String ss;
String[] arr;
String s;
String[] results;

public GameCharacter(int i,String str,String[] s)
{
num = i;
ss = str;
arr = s;

}

int getPower()
{
return num;
}

String getType()
{
return ss;
}

String[] getWeapons()
{
for(int i=0;i<arr.length ;i++)
{
results = new String[arr.length];
results[i] = arr[i];
System.out.println(results[i]);
}
return results;
}
}



class GameSaver
{

public static void main(String[] args)
{

GameCharacter one = new GameCharacter(50,"ELF",new String[] {"bow","sword","dust"});
GameCharacter two = new GameCharacter(200,"TROLL",new String[] {"bare hands","big ax"});
GameCharacter three = new GameCharacter(120,"MAGICIAN", new String[] {"spells","invisibility"});



try
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
os.writeObject(one);
os.writeObject(two);
os.writeObject(three);
os.close();

}
catch(Exception e)
{
e.printStackTrace();
}






try
{
ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));

GameCharacter oneAgain = (GameCharacter)is.readObject();
GameCharacter twoAgain = (GameCharacter)is.readObject();
GameCharacter threeAgain = (GameCharacter)is.readObject();



System.out.println(oneAgain.getPower() + "," + oneAgain.getType()+ "," + oneAgain.getWeapons() );
System.out.println(twoAgain.getPower() + "," + twoAgain.getType() + "," + twoAgain.getWeapons() );
System.out.println(threeAgain.getPower() + "," + threeAgain.getType()+ ","+ threeAgain.getWeapons() );


is.close();
}
catch(Exception e)
{
e.printStackTrace();
}


}
}


</CODE>




the output of this code should be

50,ELF,bow sword dust
200,TROLL,bare hands big ax
120, MAGICIAN,spells invisibility


But I'm not getting the required thing.
The problem is coming when I'm calling the getWeapons() method.it is actually printing some form odf address.

The problem i think is with returning arrays.I've tried a lot but failed.
Please help me out.

Thanking you ,
Jignesh
 
Ram Kumar Subramaniam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to give you some pointers. This should get your problems solved.

1.) Implement a toString method for your class
2.) Verify this part of your code - the getWeapons() method

[ February 12, 2005: Message edited by: Ram Kumar Subramaniam ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the BB tags (including CODE) use square brackets instead of angle brackets to be different from HTML. You can also edit your posts by using the pencil and paper icon (second from far right in your post header).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!