5 Replies - 503 Views - Last Post: 31 March 2016 - 04:33 PM Rate Topic: -----

#1 Dwarfcally  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-March 16

help figuring out what's wrong with Binary file progra

Posted 30 March 2016 - 07:41 PM

I'm trying to make a program to take data from memory, then write it to a binary file. It involves 2 classes, one JDialog, and then the main program.

Right now I'm getting stuck on the memory to text file part. It's not picking up the user inputted data, and I'm not sure how to code it

         try
         {  
            FileOutputStream fout = new FileOutputStream("myfile.dat");
            DataOutputStream out = new DataOutputStream(fout);
     
            System.out.println("Writing array into myfile");
            
            // tokenizing data string
             String data = getData();
             System.out.println(tools.toolArray[0]);
             System.out.println(data);
             StringTokenizer tokens = new StringTokenizer(data, ", ");
             int numToks = tokens.countTokens();
             String[] tokenArray = new String[numToks];
             int i = 0;
             
           // creating tokens in loop
            while (tokens.hasMoreTokens()){
            data = tokens.nextToken();
            tokenArray[i] = data;
            System.out.println(tokenArray[i] + i);
            i++;
            }
            
            
           // Loop to write everything to file
            while(tokens.hasMoreTokens())
            { 
            
                 System.out.println("Printing to file..." + tokens.nextToken());
                 out.writeUTF(tokens.nextToken());

            }
            
      
      System.out.println("Done");
      
      out.close(); 
         }
        catch ( IOException ioe)
         {
            JOptionPane.showMessageDialog(this, ioe.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
         }




This is the snippet in question. All data inputted by the user (toolName, ToolID, quality, numberInStock, basePrice) go into toolArray[], and this program is supposed to retrieve all the data entered and put them into a binary sequential text file. When I input the data, it does go into memory (as seen when you use the display data option on the Tim's Warehouse JFrame), but then this snippet doesn't pick it up and just puts out nulls and 0s, or ridiculously long strings of unreadable text.

Here's the link for the first TOOLS.class: http://pastebin.com/8K9qeBqk

Second class: http://pastebin.com/80pFB9hx

JDialog for user input (originally the stand-alone program): http://pastebin.com/NY5QWzcX

The program: http://pastebin.com/P0nrVhnk

The problem occurs around line 180, where I'm creating the tokens and loops to write to myfile.dat. Everything seems to be working, but the data isn't correct. Try opening the JDialog with Interactive Processing button, enter some simple data, then click Save Binary File, then choose myfile.dat. If anyone can help me figure this out I'd be so thankful, I've been working on this for 7+ hours every day for the past week and absolutely cannot seem to make any progress. If you have any other questions let me know and I'll do my best to answer them

This post has been edited by andrewsw: 31 March 2016 - 02:09 AM
Reason for edit:: removed "Java" from topic title, it is redundant


Is This A Good Question/Topic? 0
  • +

Replies To: help figuring out what's wrong with Binary file progra

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: help figuring out what's wrong with Binary file progra

Posted 30 March 2016 - 11:48 PM

Quote

   // Loop to write everything to file
            while(tokens.hasMoreTokens())

It won't have any more tokens - because you've already iterated it. That's the problem. Had you observed the API docs and refrained from using the legacy class StringTokenizer in favour of String.split, you would not have had this problem, since you wouldn't have been constrained into an iterator pattern of usage.

But let's back up here - quite a long way: in what way do you think you have a binary file here? What you have is an unnecessarily complex text file actually with some 'binary' metadata, just to make that file more difficult to read/write ;)
Do you need it to be a binary file, and if so, why?

This post has been edited by g00se: 30 March 2016 - 11:50 PM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 Dwarfcally  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-March 16

Re: help figuring out what's wrong with Binary file progra

Posted 31 March 2016 - 12:00 AM

We were never told how to use split, and were told Tokenizer was fine enough to use. We're also told to use a binary file for this project, taking user data and turning it into binary sequential form. How did I not make the file right? I got it print earlier, but the data still came out wrong.it's using the values I set in ToolItems which are nulls and 0s, instead of the updated data after user input. That's what I'm stuck on, getting the new values instead of default null ones
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: help figuring out what's wrong with Binary file progra

Posted 31 March 2016 - 12:13 AM

Quote

How did I not make the file right?

I can't answer that fully, because i'm having to guess the spec. Had you posted it, i wouldn't have to guess.
You were possibly meant to serialize (google that with 'Java') ToolItem[] to file, which would produce a binary file. That class could have been better named Tool btw, and as far as naming is concerned generally, see http://technojeeves....yle-conventions

This post has been edited by g00se: 31 March 2016 - 01:00 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: help figuring out what's wrong with Binary file progra

Posted 31 March 2016 - 02:11 AM

Try the following. It doesn't invalidate my earlier comments, rather it reimplements your existing code

    public void SaveBSAFile() {
        DataOutputStream out = null;
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);

        int code = jfc.showSaveDialog(this);

        if (code == JFileChooser.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "File not saved");
        }

        File myfile = jfc.getSelectedFile();

        if ((myfile == null) || myfile.getName().equals("")) {
            JOptionPane.showMessageDialog(null, "Invalid file name",
                "fileInfo", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "File open");

            try {
                out = new DataOutputStream(new FileOutputStream("myfile.dat"));

                System.out.println("Writing array into myfile");

                // tokenizing data string
                String data = tools.getData();
                System.out.println(tools.toolArray[0]);
                System.out.println(data);

                String[] tokenArray = data.split("\\s*,\\s*");

                // creating tokens in loop
                for (int i = 0; i < tokenArray.length; i++) {
                    System.out.println(tokenArray[i] + i);
                }

                // Loop to write everything to file
                for (int i = 0; i < tokenArray.length; i++) {
                    System.out.println("Printing to file..." + tokenArray[i]);
                    out.writeUTF(tokenArray[i]);
                }

                System.out.println("Done");
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, ioe.toString(), "Error",
                    JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            } finally {
                try {
                    out.close();
                } catch (Exception e) { /* ignore */
                }
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#6 Dwarfcally  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-March 16

Re: help figuring out what's wrong with Binary file progra

Posted 31 March 2016 - 04:33 PM

oh my god I figured it out! It was way simpler than I thought it would be.

 public void SaveBSAFile()
 {
   JFileChooser jfc = new JFileChooser();
   jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
   int code = jfc.showSaveDialog(this);
   
   if (code == JFileChooser.CANCEL_OPTION)
   JOptionPane.showMessageDialog(null, "File not saved");
   
   
   File myfile = jfc.getSelectedFile();
   
   if (myfile == null || myfile.getName().equals(""))
      JOptionPane.showMessageDialog(null, "Invalid file name", "fileInfo", JOptionPane.ERROR_MESSAGE);
   else
      {
         JOptionPane.showMessageDialog(null, "File open");
       
         try
         {        
            FileOutputStream fout = new FileOutputStream("myfile.dat");
            DataOutputStream out = new DataOutputStream(fout);
     
            System.out.println("Writing array into myfile");
            System.out.println(tools.toolArray[0].getName());
            System.out.println(tools.numberOfItems);
            
            for(int i=0; i<=tools.numberOfItems-1; i++)
            {
               System.out.println("Printing " + tools.toolArray[i].getName());
               out.writeUTF(tools.toolArray[i].getName());
               System.out.println("Printing " + tools.toolArray[i].getID());
               out.writeInt(tools.toolArray[i].getID());
               System.out.println("Printing " + tools.toolArray[i].getQuality());
               out.writeInt(tools.toolArray[i].getQuality());
               System.out.println("Printing " + tools.toolArray[i].getNumberInStock());
               out.writeInt(tools.toolArray[i].getNumberInStock());
               System.out.println("Printing " + tools.toolArray[i].getPrice());
               out.writeDouble(tools.toolArray[i].getPrice());
            }

      System.out.println("Done");
      
      out.close();
      fout.close(); 
         }
        catch ( IOException ioe)
         {
            JOptionPane.showMessageDialog(this, ioe.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
         }
       }



I never thought to use the get() methods to get the values I wanted, but this works and it prints to the file in binary. Thank you for the help, I really appreciate it. The serialization was something we never went over and it helped me understand it a little bit better as well. I'm still unsure on how split() works, I'll have to ask my teacher why we don't learn it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1