Results 1 to 11 of 11
Thread: InputStream problem
- 06-29-2010, 11:50 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
InputStream problem
I have written 3 arrays to disc using:-
public void saveData(){
try{FileOutputStream f_out= new FileOutputStream("mydata.zzz");
ObjectOutputStream obj_out =new ObjectOutputStream (f_out);
obj_out.writeObject(scores);
obj_out.writeObject(props);
obj_out.writeObject(things);}
catch (IOException e)
{System.out.println(" A write error has occurred");
When I try to read them back I am having problems with the 4thline of code
getting javac errors.
I have tried the following-errors shown alongside each try.
What should it be?
Thanks
FileInputStream f_in = new FileInputStream ("mydata.zzz");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
Object obj=obj_in.readObject();
// scores[][] =obj[][];
//scores =obj; error required int[][]
//scores =int[][] obj; error .class expected
- 06-29-2010, 01:25 PM #2
What is the definition of scores?
Can you post the FULL text of the error messages that show the source and its error.
You've edited out some info.
- 06-29-2010, 01:39 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
score is a 2 dim array defined in class Buttons -see below- I am trying to save and restore the three arrays scores, props and things. \the first line I tried
scores=obj; gave the error incompatible types found:java.lang.Object
required int [][] with pointer under the o of obj
I then tried scores=int[][]obj;
This gave error '.class' expected
scores=int[][]obj;with pointer again under the o of obj followed by //error .class expected
Java Code:import java.awt.*; import java.awt.event.*; // Mon 28June works OK class Buttons extends JFrame implements ActionListener, java.io.Serializable {JDesktopPane desktop; JPanel pnl = new JPanel(); //ClassLoader ldr = this.getClass().getClassLoader(); JPanel questPanel = new JPanel(); JButton certBtn = new JButton( "Yes Certain" ); JButton thinkBtn = new JButton( "I think so" ); JButton notSureBtn = new JButton( "Don't Know" ); JButton noThinkBtn = new JButton( "Don't think so" ); JButton noBtn = new JButton( "No" ); boolean validQuest=false; boolean flag=false; boolean questSet=false; JMenuItem menuItemNew = new JMenuItem("New"); JMenuItem menuItemLoad = new JMenuItem("Load"); JMenuItem menuItemSave = new JMenuItem("Save"); JMenuItem menuItemSaveAs = new JMenuItem("Save as"); JMenu menu = new JMenu("File"); JMenu menu2 = new JMenu("Disp"); JMenuBar menuBar =new JMenuBar(); JMenu menu1 = new JMenu("Mode"); JMenuItem menuItemDisplay = new JMenuItem("Display"); JMenuItem menuItemTeach = new JMenuItem("Teach"); JMenuItem menuItem3 = new JMenuItem("Question"); int ansCode; JTextArea txtArea = new JTextArea(5,40); String myteststr; String props[] = new String[50]; String splits[] = new String[50]; String things[]=new String[100]; int scores[][]=new int[100][50]; int noOfProps; int noOfThings; int questNo;
Last edited by Fubarable; 06-29-2010 at 01:44 PM. Reason: Moderator edit: code tags added to aid readability
- 06-29-2010, 01:46 PM #4Can you post the FULL text of the error messages that show the source and its error.
You've edited out some info.
Java Code:Example.java:4: ']' expected int scores[][4]=new int[100][50]; ^ Example.java:4: ']' expected int scores[][4]=new int[100][50]; ^
-
1) You haven't posted the full error message, just a partial one.
2) Don't you have to do some casting here?
3) I've added code tags to your code posted above. To see how to do this yourself, please click on the link in my signature.
Luck!
- 06-29-2010, 02:07 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
I can't get a screen copy of the errors to indicate where the ^ is placed but the errors are in the line where I try to take the read obj and restore the array with that information
score is a 2 dim array defined in class Buttons -see below- I am trying to save and restore the three arrays scores, props and things.
The first line I tried
scores=obj; gave the error
Buttons.java:125: incompatible types
found:java.lang.Object
required int [][]
scores =obj
^
I then tried scores=int[][]obj;
This gave error
Buttons.java:124: '.class' expected
scores=int[][]obj; //error .class expected
The section of code concerned
Java Code://read file here try {FileInputStream f_in = new FileInputStream ("mydata.zzz"); ObjectInputStream obj_in = new ObjectInputStream (f_in); Object obj=obj_in.readObject(); // scores[][] =obj[][]; //scores =obj; error required int[][] scores =int[][] obj; //error .class expected //scores =obj;
-
Again, per the API and the tutorial, don't you have to do some casting here? ObjectInputStream#readObject() returns an Object reference, not an int[][] reference and so casting your return result to (int[][]) ought to help greatly.
- 06-29-2010, 03:19 PM #8found:java.lang.Object
required int [][]
scores =obj
and found an object of type Object (that's the type of obj)
As Furbarable recommends, cast the variable obj to type (int[][]) to match the type of scores.
- 06-29-2010, 03:23 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
I have looked at the tutorial but am still not sure how to cast obj as an array, I thought that
scores[][] =obj[][];
would do it but that also gave error.
should I set obj=int[][]; before I read with
Object obj=obj_in.readObject();
- 06-29-2010, 03:27 PM #10how to cast obj
X aRefToX;
...
aRefToX = (X) obj; // cast obj to type X
You put the type to cast to in ().
See the above posts they both show (int[][]). Didn't you wonder why we put that in our posts?
- 06-29-2010, 04:09 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Creating an InputStream
By c26354 in forum New To JavaReplies: 10Last Post: 07-12-2011, 04:48 AM -
Reading inputstream on socket
By javanetworknew in forum NetworkingReplies: 1Last Post: 04-27-2010, 11:01 AM -
Java InputStream
By Bill88 in forum New To JavaReplies: 10Last Post: 09-21-2009, 02:40 PM -
Which InputStream takes the least memory?
By arnab321 in forum New To JavaReplies: 9Last Post: 01-13-2009, 05:45 PM -
Reading bytes from InputStream
By Java Tip in forum Java TipReplies: 0Last Post: 11-25-2007, 07:51 PM
Bookmarks