Win a copy of Create an Uber Clone in 7 Days: Build a real world full stack mobile app in Java this week in the Android forum!

Carey Brown

Saloon Keeper
+ Follow
since Nov 19, 2001
Carey likes ...
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
Forum Moderator
Carey Brown currently moderates these forums:
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Carey Brown

You'll need two nested loops. One to loop for each line in the text file (which you already have). And an inner loop that loops through the individual numbers after you've "split" the line.
14 hours ago

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.
1 day ago
I haven't run across that one. My guess is that all classes at some level inherit from the Object class though it is implied, not explicit. So super() may be calling Object's constructor.
3 days ago

Fred Kleinschmidt wrote:

David Ellis Rogers wrote:how do I perform a compile in Eclipse?

Make sure "Project->Build automatically" is checked. Then any time you save an edit it will automatically compile everything.

This works 99.99% of the time. On rare occasions the build process hiccups. The way to get back on track is selecting Project > Clean.
3 days ago

salvin francis wrote:

Carey Brown wrote:....It's best to create it as a class constant and share that constant among all of your other code.

I haven't used Scanner for a while now, but as far as I remember, it is not thread safe. Is it good to use it as a class constant ?

No, it isn't. But what about any CUI (console user interface) would make any sense in a threaded environment? Each Scanner has its own buffer and state, so if you open two Scanners to, even in a single threaded app, the keystrokes could end up in the wrong Scanner object. Even GUIs only allow a single point of keyboard focus at a time.
4 days ago

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

Yes, and avoid nulls.

4 days ago

Kevin O'Sullivan wrote:Thanks guys, I'll read up on Buffer.

I guess what I was trying to say was use Scanner instead of BufferedReader but expect a couple of nasty bumps to the shins as you try to wrap your head around the pitfalls.

To start out:
  • Only ever create a new Scanner() with once for the life time of your running project. It's best to create it as a class constant and share that constant among all of your other code.
  • Never ever close a Scanner created with, no matter what the compiler/IDE tells you.
  • Scanner#nextLine() is the only method guaranteed to absorb the ending new-line character. That's not to say don't use other methods, e.g. nextInt(), just remember they'll leave a new-line in the input stream that you WILL have to deal with.

  • Often a project contains a class whose only purpose is to be a wrapper around Scanner( Here's a blatantly simplistic class.
    Now in your project you can call
    To elaborate on this you could incorporate wrappers for each of the Scanner methods, like nextInt().
    Now in your project you can call

    4 days ago

    Matt Wong wrote:advice:
    1) get rid of scanner and use proper input like BufferedReader.nextLine
    2) ALWAYS use nextLine and perform casts and parses yourself - so tailing line break always gets cleared

    Yeah, I fought the Scanner vs BufferedReader as well as the "parse it yourself" issues for a long time. I eventually gave up. The tide of common usage was overwhelming. So now I'm reduced to educating others on how to avoid the pitfalls of Scanner. In some specific instances I return to BufferedReader but I see where sometimes Scanner has its place as well.
    4 days ago
    Screen shot is useless, too small. Can you copy-and-paste text into a post?
    4 days ago
    Line 81: What is this?
    4 days ago
    You have in a while(true) block and there's only one break inside. If you don't exercise the break you'll go back again.
    4 days ago

    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.
    4 days ago
    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.
    4 days ago
    Where is your writeFile() method?
    4 days ago
    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().
    4 days ago