Results 1 to 8 of 8
- 01-20-2009, 03:54 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
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.
What this program is? Its an assignment, a to-do list.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 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.
-
ouch. my eyes hurt.
-
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.
- 01-20-2009, 04:22 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Putting:
I get a bunch of these:Java Code:System.out.println(rank_entry); System.out.println(entry); System.out.println(check);
[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.
-
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?
-
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.
- 01-20-2009, 05:08 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
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++;...
-
Similar Threads
-
8 questions I dont understand while studying for SCJP
By shankhas in forum Java CertificationReplies: 5Last Post: 05-19-2010, 07:53 AM -
Storing and Retrieveing Data Using XStream & xml Files
By geeeeky.girl in forum New To JavaReplies: 0Last Post: 01-04-2009, 10:24 PM -
help to understand this problem clear
By beginner21 in forum New To JavaReplies: 17Last Post: 12-01-2008, 04:44 PM -
Joing data from XML files
By keioGirl in forum XMLReplies: 0Last Post: 07-03-2008, 09:06 PM -
Packaging and accessing data files
By todd in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 12:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks