Campbell Ritchie

+ Follow
since Oct 13, 2005
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 Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

Ansamana Sankarray wrote:. . . my own solution  . . .

That looks good but you  should be able to use something on the lines of b = !b; to reduce lines 18‑23 to one statement.
What do lines 26‑28 mean?
13 hours ago
Those print a triangle programs are always annoying.
Suggest you work out a formula. You need to work out the length of the word, and the number of spaces before and after the nth letter (number of spaces may be 0).
I suggest you write down how you would do that, which will be the beginning of writing your code. There is no point in even looking at code until you have worked out/thought through the algorithm you are using and the problem you are solving.
13 hours ago
. . . and does your code work? What sort of output do you get? Are you suffering any exceptions?
Don't declare your List as ArrayList; declare it as List. Don't call a List arr; it isn't an array. Call it numbers or something like that.
Why have you got the bang sign/not operator in line 3? I think that shouldn't be there.
If you think about the different kinds of collection/data structure available, which you can read about here, you will find a collection type whose size() method will tell you how many distinct elements you have.
19 hours ago
Congratulations, all four of you
And thank you JF for coming here to promote your book.
I suggest you should start by creating a search facility without a GUI.
1 day ago
Somebody suggested it is a CLASSPATH problem, and I am beginning to think that is correct. Please show us what happens if you write echo %CLASSPATH%
1 day ago
This is what I used; it is slightly different from a proper class because I used JShell.output:-
1  4  9 16 25 36 49 64 81100
I used + 1 in line 5 otherwise I'd have been adding 0 and gone into an infinite loop.
Now, knowing what we know about count of factors: why does this method create that particular series of numbers?
1 day ago

Chas Blessed wrote:. . . I never meta language I didn't like"? . . . .


Welcome to the Ranch
Welcome to the Ranch

Is this code from Head First Java by Sierra and Bates? Did you copy it form the book or download it from the book's website? The code from the website is subtly different, in that it has package declarations for each class.
1 day ago
That's a different method from what I tried. I like the counting of factors.
1 day ago
What would you do for these numbers?
1 day ago

Jay Rex wrote:. . .

That technique of creating objects is error-prone

How would I do that in a better way?

If you are reading from a text file, you are stuck with that technique. You have the awkward task of ensuring that the format of the file and the way you are reading it match. You are now coupled to the format of the file, and I think that is unavoidable.
There is another way to read things: read a whole line with your Scanner and divide it into an array with String#split; you can probably use ";" as the regex. Please check, but I think the semicolon isn't a meta‑character.

I see you are up to date with try-with-resources

Thanks to you

Gee, shucks, you got me all 'barrassed!

. . . . Are documentation comments seen in a different light?

Yes. You use the javadoc tool to create an HTML document which you open with a web browser, and you use the documentation comments to guide you about how to use the code.

. . . Where do I create the static method, inside or Is one more correct than the other? . . .

If you use a factory method to create an object of a class, it is usual to put that method in the same class. So Product in this case.

. . . What would be a better name for

There isn't a really good name for that sort of class, but how about ProductDemo?
1 day ago