Results 1 to 8 of 8
  1. #1
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Data Files - A problem that I dont understand :D

    Oubviously not the WHOLE code, but the load button. The save button is simple and works good. But the load button doesnt.

    Java Code:
        //Load Button
        if(e.getSource()==load_button){
          ////////////Data File Done//////////////
          String [] fields; // array to store the "split" line read
          
          BufferedReader in=null; //variable representing the bufferedreader
          String line="1 A 2"; //variable to read each line from the data file
          File f=new File("todolist.txt"); //variable reprsenting the data file
          
          //Openening Data File
          try
          {
            in=new BufferedReader(new FileReader(f));
            System.out.println("Data File Process.");
          }
          catch (FileNotFoundException d)
          {
            System.out.println("Error.");
          }
          
          
          //Reading Lines and Placing into Variables
          while(line!=null)
          {
            try{
              line=in.readLine();
              if (line!=null)
              {
               [B] 
                fields=line.split(",");
                rank_entry[0]=Integer.parseInt(fields[0]);
                entry[0]=fields[1];
                check[0]=Integer.parseInt(fields[2]);[/B]
              }
            }
            catch (IOException d)
            {
              System.out.println("Error.");
            }
            
          } 
          //Data File Closure
          try
          {
            in.close();
            System.out.println("Data File Process.");
          }
          catch (IOException d)
          {
            System.out.println("Error."+e);
          }
          //Actual Input into Program
          for (int q=0;q<15;q++){
            field[q].setText(entry[q]);
            rank[q].setText(Integer.toString(rank_entry[q]));
            if (check[q]==1){
              done[q].setSelected(true);
              field[q].setEnabled(false);
              rank[q].setEnabled(false);
              done[q].setFont(new Font("Tahoma", Font.BOLD, 10));
            }
            else{
              done[q].setSelected(false);
              field[q].setEnabled(true);
              rank[q].setEnabled(true);
              done[q].setFont(new Font("Tahoma", Font.PLAIN, 10));
            }
          }
        }
    What this program is? Its an assignment, a to-do list.

    What are some of the objects?
    rank and field (15 of each), are text fields where the user enters info in
    done (15 again) are checkboxes to set if activity is done or not

    rank and check are int arrays (0-15 slots) that are used with the data file
    entry is another string array (again 15) that is used for the data file

    Problem: When saving the stuff and then loading the info from the text works magnificiantly. Once you close the program and run it and press load, only the first thing is filled up. Oubviously because of the [0].

    General Focus: The bolded part of the code is where I think the problem is, I set it to [0] but theres actually 15. But setting a loop doesnt work because the data file reads it in order without any loop.

    What do I do?: Because the 0 doesnt work, what do I set there?
    Last edited by Exhonour; 01-20-2009 at 04:28 AM.

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

    Default

    ouch. my eyes hurt.

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

    Default

    1) you need better catch blocks. At least print out the stack trace via d.printStackTrace();
    2) Create a small program that tests just this part of the program: It should open a file for input, read it in line by line and write it out on the console.
    3) Fill this test program with multiple println statements so you know what is working and what isn't working.
    4) Can you read in with a scanner object? It may be easier to work with.
    5) Do you need to trim your input before parsing it?

    In short, you need to do some heavy-duty debugging.

  4. #4
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Putting:
    Java Code:
                System.out.println(rank_entry);
                System.out.println(entry);
                System.out.println(check);
    I get a bunch of these:
    [I@24be0446
    [Ljava.lang.String;@39bde3d2
    [I@4dc67b54

    I looked at the code tons of times its not getting to me ...
    Last edited by Exhonour; 01-20-2009 at 04:28 AM.

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

    Default

    You're calling println on arrays, so without care you'll get what looks like nonsense. Note that:
    [I + hex address stands for an int array and
    [Ljava.lang.String + hex address stands for a String array.

    Better to either call println on the individual array items, such as
    System.out.println(entry[whatever]), or to get the whole thing
    System.out.println(Arrays.toString(entry));

    also, shouldn't you use an int index for your arrays and increment this index with each round through the loop here instead of always using 0?

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

    Default

    Myself, I'd create a RankEntryCheck class, make an ArrayList for these objects and every loop through the file, I'd create a new RankEntryCheck object and then add it to my List.

  7. #7
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Fixed it. But wouldnt it be simple for u to just tell me this: (?) It is new to java forum and id like something i can understand easily :D
    ...int flag=0;...

    ...while(line!=null && flag<15)...

    ...rank_entry[flag]=Integer.parseInt(fields[0]);...
    ...entry[flag]=fields[1];...
    ...check[flag]=Integer.parseInt(fields[2]);...

    ...flag++;...

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

    Default

    um,... are you criticizing me for trying to help?
    Last edited by Fubarable; 01-20-2009 at 05:38 AM.

Similar Threads

  1. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 07:53 AM
  2. Storing and Retrieveing Data Using XStream & xml Files
    By geeeeky.girl in forum New To Java
    Replies: 0
    Last Post: 01-04-2009, 10:24 PM
  3. help to understand this problem clear
    By beginner21 in forum New To Java
    Replies: 17
    Last Post: 12-01-2008, 04:44 PM
  4. Joing data from XML files
    By keioGirl in forum XML
    Replies: 0
    Last Post: 07-03-2008, 09:06 PM
  5. Packaging and accessing data files
    By todd in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 12:27 AM

Posting Permissions

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