• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRecordCount

 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the below code. It compiled and when I run it, my main.getRecordCount() is not updated and it should because the main.create() creates a new row to the database.
Here is my code:

String[] a={VALID_RECORD+"","Dogs with To ","Whoville","aa",null,null,"bb"};
int n=main.create(a);
System.out.println("created row "+n);

for(int r=1;r<main.getRecordCount();r++){
String[] str=main.read(r);
StringBuffer rv = new StringBuffer("recordNumber = " + r +"\n");
for (int i = 0; i < main.fieldInfo.length; i++) {
rv.append( main.fieldInfo[i].getName()+" "+" = " + str[i]+"\n");
}
System.out.println(rv.toString());
}

Please help, anyhelp will be appreciated,
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you create to and read from the same target? Maybe you create in a cache without flushing into disk but read from physical disk. Don't know how you implement your create and read.
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
public int create(String [] data) throws DuplicateKeyException;

// Reads a record from the file. Returns an array where each
// element is a record value.
public String [] read(int recNo) throws RecordNotFoundException;

[Andrew: put the source code between [code] and [/code] UBB tags]

[ September 13, 2004: Message edited by: Andrew Monkhouse ]
[ September 13, 2004: Message edited by: David Dong ]
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help everyone, by the way Thanks Andy !
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, David:

first you may want to quote your code in so that it has original format. Now reading your code is somehow awkward.

I didn't read thru your code. But when you copy your data for name and location, you use index 1 and 2. As I understand, they should be indexed 0 and 1. (flag is in disk, and not a field in the data format specified that is why data[0] for name). This is from my instruction and interpretation. Read yours carefully and may solve the problem.
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking to have flag as a separate part from the field but I think it is better to count flag as a field.
by the way Which assignment do you have?

are you able to solve the mystery of why my getRecordCount isn't updated, when it's run after create ?
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see nowhere you did update record count. your recordCount is a local variable. you have getRecordCount() which I don't know if you increment counter after getting one or you have somewhere to set record count, or you have an some sort of instance variable to keep the record for you.

Anyway, I didn't find in your code you try to update counter.
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry ,
I have the getRecordCount by reading the database.
I made a create a new row to the database, and it seems like it gets updated before getRecordCount being called.
But getRecordCount gets the old count instead of the oldCount + 1.

Do you understand better now?
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy,
I solved it. It was the for loop. Thank you very much for your help..
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11943
211
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which hopefully makes the code easier to read. I did not change your indenting though.

When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.

If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.

Regards, Andrew
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh.. I didn't know. Thanks for letting me know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic