Results 1 to 8 of 8
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] Array help

    :eek:

    :confused:

    I have a project underway, I am setting an array so that the records from the txt file can be read/updated.

    I have already completed a smaller project that works fine, however for some reason I can't transfer that information.

    The error I am getting is telling me is 'create method'

    Java Code:
    record = MshipRecord( // this is the error
    from this section of code

    Java Code:
    while(arraynum <= recnum) {
                         try {
                             record = MshipRecord(
                                     stMembership[arraynum],
                                     stJoinDate[arraynum],
                                     stBmi[arraynum]);
                             output.writeObject(record);
                         }
                     catch (IOException e){
                            optPane1.showMessageDialog(this, "Error Adding Record", "Error", optPane1.ERROR_MESSAGE);
                      }
                         arraynum = arraynum +1;

    the above section of code is out of the following, it is the last while loop.

    Java Code:
    private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
           // Update Records, edit a detail such as age then press update.
    
            recnum = 0; // sets record number count to 0
            boolean endofFile = false;
            MshipRecord record;
            boolean openOK = true;
                try{
                    input = new ObjectInputStream( new FileInputStream(file));  // open file to read
                    }
                    catch (IOException e)  { // catch open error
                    optPane1.showMessageDialog(this, "Error in File", "Error",optPane1.ERROR_MESSAGE);
                    openOK =false;}
    
    
            // if file opened ok
                    if (openOK == true) { // if file opened OK
                        while (endofFile == false){ // while not eof
                    try{
                        record =(MshipRecord)input.readObject(); //read record into an arrary
                        stMembership[recnum]=record.getMbrship(); // CHECKED UPTO HERE
                        stJoinDate[recnum]=record.getRdate();
                        stBmi[recnum]=record.getBmindex();
                    }
                    catch (EOFException e) {//catch eof, may not be an error
                        endofFile =true;
                    }
                    catch (IOException e) {
                        optPane1.showMessageDialog(this, "Error in File", "Error", optPane1.ERROR_MESSAGE);
                    }
                    catch (ClassNotFoundException e){//catch class record error
                        optPane1.showMessageDialog(this, "Class not found", "ERROR", optPane1.ERROR_MESSAGE);
                    }
                    recnum = recnum + 1;
                    }
                    recnum = recnum - 1;
                    System.out.println("recnum" + recnum);
    
    
                    //
                    int arraynum=0;
        boolean found=false;
                    System.out.println("recnum,arraynum" + recnum + "/" + arraynum);
                    System.out.println(found);
    
        while((arraynum <= recnum) && (found == false)){
                    System.out.println("arraynum" + arraynum);
                    if (stMembership[arraynum].equals(txtMembership.getText())){ // is this a match ?
                        stJoinDate[arraynum]=txtJdate.getText();// display stored fields
                        stBmi[arraynum]=Double.parseDouble(txtBmi.getText());
                        found = true;
                    }
                    arraynum = arraynum + 1;
                    }
                    System.out.println("arraynum end" + arraynum);
    
                    // open file to write, write all file out from array
                    boolean newok = true;
                    try {
                        output = new ObjectOutputStream(new FileOutputStream(file));
                    }
                    catch (IOException e) { //catch new file error
                        newok = false;
                    }
    
                    // MshipRecord Record
                    if (newok = true) { // if new file created OK
                        arraynum = 0;
                     while(arraynum <= recnum) {
                         try {
                             record = MshipRecord(
                                     stMembership[arraynum],
                                     stJoinDate[arraynum],
                                     stBmi[arraynum]);
                             output.writeObject(record);
                         }
                     catch (IOException e){
                            optPane1.showMessageDialog(this, "Error Adding Record", "Error", optPane1.ERROR_MESSAGE);
                      }
                         arraynum = arraynum +1;
                     }
    
                    }
    
                    }
    I have a class called MshipRecord and have totally lost it after looking at this code slowly ironing out the errors but this one is defeating me.

    Thanks in advance for any help.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    record = new MShipRecord(...);

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Array

    Maybe im just being thick but I dont get what you are trying to tell me, the following works fine in another project.

    Java Code:
     // Update records for movies
        // Open file to read & read all file into an array
    
    recnum = 0; // set the record number count to 0
    boolean endofFile = false;
    videoRecord record;
    boolean openOk = true;
    
        try{
            input = new ObjectInputStream( new FileInputStream(file));  }
                catch (IOException e)  {
               optPane1.showMessageDialog(this, "error in file", "File Error",optPane1.ERROR_MESSAGE);
               openOk =false;}
            // TODO add your handling code here:
    if (openOk == true) { // if file opened OK
        while (endofFile == false){ // while not eof
            try{
                record =(videoRecord)input.readObject(); //read record into an arrary
                stSection[recnum] = record.getClassification();
                stClassification[recnum] = record.getClassification();
                stTitle[recnum] = record.getTitle();
                //stsection[recnum] = record.getCode();
                stDur[recnum] = record.getDuration();
                stHireCost[recnum] = record.getHireCost();
            }
            catch (EOFException e) {//catch eof, may not be an error
                endofFile =true;}
            catch (IOException e) {
                optPane1.showMessageDialog(this, "Error in File", "Error", optPane1.ERROR_MESSAGE);
            }
            catch (ClassNotFoundException e){//catch class record error
                 optPane1.showMessageDialog(this, "Class not found", "ERROR", optPane1.ERROR_MESSAGE);
        }
        recnum = recnum +1; // adjust for last loop eof
        System.out.println("recnum" + recnum);
    }
    
        int arraynum=0;
        boolean found=false;
        System.out.println("recnum,arraynum" + recnum + "/" + arraynum);
        System.out.println(found);
    
        while((arraynum <= recnum) && (found == false)){
            System.out.println("arraynum" + arraynum);
            if(stTitle[arraynum].equals(txtTitle.getText())) { // is this a match?
                stSection[arraynum]=txtSection.getText(); // display stored fields
                stClassification[arraynum]=txtClassification.getText();
                //stCode[arraynum]=txtCode.getText();
                stDur[arraynum]=Integer.parseInt(txtDuration.getText());
                stHireCost[arraynum]=Float.parseFloat(txtHireCost.getText());
                found=true;
            }
            arraynum = arraynum +1;
        }
        System.out.println("arraynum end" + arraynum);
    
        // open file to write, write all file out from array
    
        boolean newok = true;
        try {
            output = new ObjectOutputStream(new FileOutputStream(file));
        }
        catch (IOException e) { // catch new file error
            newok=false;
        }
        // videoRecord record
        if (newok=true){ //if new file created OK
            arraynum = 0;
          while (arraynum <= recnum){
              try {
                  record=new videoRecord(
                          stSection[arraynum],
                          stClassification[arraynum],
                          stTitle[arraynum],
                          stDur[arraynum],
                          stHireCost[arraynum]);
                  output.writeObject(record);
              }
              catch (IOException e){
                  optPane1.showMessageDialog(this, "Error Adding Record", "Error", optPane1.ERROR_MESSAGE);
              }
              arraynum = arraynum + 1;
          } //back to while loop
        }//if new file created OK
        }//if file not open OK at first
    So why am I having the issue I am having with the above?

    Thanks in advance

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    to call a constructor, you need to use the "new" keyword.

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Array

    But what I am saying is why didnt I have to do that in the code above for videoRecord?

    Java Code:
    // Update records for movies
        // Open file to read & read all file into an array
    
    recnum = 0; // set the record number count to 0
    boolean endofFile = false;
    videoRecord record;
    boolean openOk = true;
    
        try{
            input = new ObjectInputStream( new FileInputStream(file));  }
                catch (IOException e)  {
               optPane1.showMessageDialog(this, "error in file", "File Error",optPane1.ERROR_MESSAGE);
               openOk =false;}
            // TODO add your handling code here:
    if (openOk == true) { // if file opened OK
        while (endofFile == false){ // while not eof
            try{
                record =(videoRecord)input.readObject(); //read record into an arrary
                stSection[recnum] = record.getClassification();
                stClassification[recnum] = record.getClassification();
                stTitle[recnum] = record.getTitle();
                //stsection[recnum] = record.getCode();
                stDur[recnum] = record.getDuration();
                stHireCost[recnum] = record.getHireCost();
            }
            catch (EOFException e) {//catch eof, may not be an error
                endofFile =true;}
            catch (IOException e) {
                optPane1.showMessageDialog(this, "Error in File", "Error", optPane1.ERROR_MESSAGE);
            }
            catch (ClassNotFoundException e){//catch class record error
                 optPane1.showMessageDialog(this, "Class not found", "ERROR", optPane1.ERROR_MESSAGE);
        }
        recnum = recnum +1; // adjust for last loop eof
        System.out.println("recnum" + recnum);
    }
    
        int arraynum=0;
        boolean found=false;
        System.out.println("recnum,arraynum" + recnum + "/" + arraynum);
        System.out.println(found);
    
        while((arraynum <= recnum) && (found == false)){
            System.out.println("arraynum" + arraynum);
            if(stTitle[arraynum].equals(txtTitle.getText())) { // is this a match?
                stSection[arraynum]=txtSection.getText(); // display stored fields
                stClassification[arraynum]=txtClassification.getText();
                //stCode[arraynum]=txtCode.getText();
                stDur[arraynum]=Integer.parseInt(txtDuration.getText());
                stHireCost[arraynum]=Float.parseFloat(txtHireCost.getText());
                found=true;
            }
            arraynum = arraynum +1;
        }
        System.out.println("arraynum end" + arraynum);
    
        // open file to write, write all file out from array
    
        boolean newok = true;
        try {
            output = new ObjectOutputStream(new FileOutputStream(file));
        }
        catch (IOException e) { // catch new file error
            newok=false;
        }
        // videoRecord record
        if (newok=true){ //if new file created OK
            arraynum = 0;
          while (arraynum <= recnum){
              try {
                  record=new videoRecord(
                          stSection[arraynum],
                          stClassification[arraynum],
                          stTitle[arraynum],
                          stDur[arraynum],
                          stHireCost[arraynum]);
                  output.writeObject(record);
              }
              catch (IOException e){
                  optPane1.showMessageDialog(this, "Error Adding Record", "Error", optPane1.ERROR_MESSAGE);
              }
              arraynum = arraynum + 1;
          } //back to while loop
        }//if new file created OK
        }//if file not open OK at first

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

    Default

    Because your getting your object ready-made via serialization in the second example. The first bit of code you are posting is completely different. Believe me, emceenugget is not steering your wrong. You'd best follow his advice.

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Arrays

    No i believe whats being said, what I am saying is that I dont understand, maybe someone could put it in a way that I understand,

    for example the (...), whats going in those brackets?

  8. #8
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Arrays, Thanks

    emceenugget, thanks, sorry it took a while for the penny to drop but your solution did work, I have just been staring at this code all day.

    Java Code:
    record = new MshipRecord(
    so everything works as it should now.

    Thanks again emceenugget & fubarable

Similar Threads

  1. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  2. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM

Posting Permissions

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