Results 1 to 11 of 11
  1. #1
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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.

  3. #3
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    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;
    Moderator edit: code tags added to aid readability
    Last edited by Fubarable; 06-29-2010 at 01:44 PM. Reason: Moderator edit: code tags added to aid readability

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Can you post the FULL text of the error messages that show the source and its error.
    You've edited out some info.
    For example:
    Java Code:
    Example.java:4: ']' expected
    int scores[][4]=new int[100][50];
                 ^
    Example.java:4: ']' expected
    int scores[][4]=new int[100][50];
                            ^
    I don't see where in the code you've posted the error is?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  6. #6
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    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;

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    found:java.lang.Object
    required int [][]
    scores =obj
    This error says the compiler was looking for an object of type int[][] (that's the type of scores)
    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.

  9. #9
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    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();

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    how to cast obj
    Do you have a textbook that shows you how to cast an object ref to a specific type?
    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?

  11. #11
    javabarn is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Smile Problem solved

    :)Many thanks problem solved and tested = it actually reads my array back

Similar Threads

  1. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 04:48 AM
  2. Reading inputstream on socket
    By javanetworknew in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 11:01 AM
  3. Java InputStream
    By Bill88 in forum New To Java
    Replies: 10
    Last Post: 09-21-2009, 02:40 PM
  4. Which InputStream takes the least memory?
    By arnab321 in forum New To Java
    Replies: 9
    Last Post: 01-13-2009, 05:45 PM
  5. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •