• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

search specific data using ObjectInputStream

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 6 you have a Car list and a Car array, which is it? How was Car list/array written to the file in the first place?

We'd need to see the source for Car and the source for where the data was being written.
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//this is my car class
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got an array and and index into the array so data should be the i'th element in the array followed by getRegNum().
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would think you'd want to search the static Car[] in the class and not the file. If someone wants to search the file then they should call readFile() followed by a call to search(). Search should not be responsible for reading as well. The way you have it there is the potential with search to populate the array without populating nextId.
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You've got an array and and index into the array so data should be the i'th element in the array followed by getRegNum().


This will barf when the i'th element is null.
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i should write a method that search in the array ?
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lee ji eun wrote:so i should write a method that search in the array ?

Yes, and avoid nulls.

 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the late reply. i will try it soon. thank you so much
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//this is what i do but still cannot output anything
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


// this is my main program
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lee ji eun wrote:

Line 3: Could "regNum" be null? possible but not likely. Even if it is null, checking it 100 times inside the loop isn't going to make a difference.

On the other hand (line 4) could carList[i] be null? Absolutely, even guaranteed.

"car" is a reference to a Car object. "carList" is an array of references to Cars. So after line 4, "car" now contains the same reference as carList[i]. You can test "car" to see if it's null and you can print "car" on line 5.
 
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to find the regNum so you would loop through the array, and check if the car value's regNum matches value you passed in.
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, but i cannot output anything
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried debugging the app to see if you can find the problem?
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you trying to do?
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no i dunno how to do that
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What IDE are you using?
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jcreator
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version are you using because LE doesn't have a debugger apparently.
 
lee ji eun
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDK version 1.8.0_92
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If debugging is a problem why not add System.out.println() calls, printing out values so you can at least see what the flow is, and what value everything has.
 
Marshal
Posts: 80617
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:Which version are you using

I presume you meant which version o JCreator not of the JDK.

. . . LE doesn't have a debugger apparently.

Does that mean JCreator Lite?
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be lite.   Either way i think learning how to use a debugger is worth it over just using print statements.  Still not really sure what hes trying to do with the code.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:It might be lite.   Either way i think learning how to use a debugger is worth it over just using print statements.


There is one school of thought that considers skills with a debugger a Bad Thing. They prefer learning how to write unit tests that will fail when there's a bug in your code. Also, learning how to write tests up front so that you have a failing test if your code is missing some kind of functionality (you could also think of missing functionality as a "bug")
 
Al Hobbs
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many tests are good to write?  Like one per public method or what is a good gauge to know if you have enough or too many?
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At least one per public method.  You should test all sorts of input and output.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a test of one method from an old project of mine.  Notice that it tests all sorts of behavior:
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
reply
    Bookmark Topic Watch Topic
  • New Topic