Phil English

Ranch Hand
+ Follow
since Jun 18, 2012
Phil likes ...
Netbeans IDE MySQL Database Ubuntu
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Phil English

Indeed. But on every line it tries to read the first character. What is the first character on an empty line?
10 years ago
Looks like it is having difficulty with one of you line variables. If it was me I'd put System.out.println(line) in before the if statement and see if I could spot the issue. Specifically, what does your code do if it encounters an empty line?

See also this post which is actually one of the 'similar thread' links below
10 years ago
What does each column/row correspond to? When I came across this problem the issue that Fred highlighted made me feel that the array-of-arrays seemed clumsy for my purpose. It transpired that each row was in fact a Point with an X state and a Y state corresponding to the columns. So rather than an array of 2-element arrays I created an array of Point objects. Could be overkill sometimes but makes things much cleaner.
10 years ago
Apologies if duplicate - I couldn't see anything. Interesting BBC article on making outsourcing work for you (short term in this instance)

BBC
11 years ago
If you don't explicitly tell the compiler what privileges are required to access a method the compiler will assign default (more properly known as package-private). If your interface mandates that the method is public then you are restricting access because default allows more limited access than public see here for more info on access modifiers

Try to set you method void display() to public void display() and see if that helps.
11 years ago

Tony Docherty wrote:With regard to that article: It is misleading to state that the pool stores values from -128 to 127.
The JLS states that auto-boxing of numbers in the range of -128 to 127 will result in the same object but goes on to say "Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.". Therefore you should not rely on the fact that numbers outside the range of -128 to 127 will be different objects.

Also, you can get the cached Integer objects if you use Integer.valueOf(x) ie:

i1 and i1 are not the same objects whereas i3 and i4 are the same objects.



Also you can pass a parameter to the JVM on instantiation to force an increase in the upper bound of the integer pool. Discussed in this thread.
11 years ago

Russell Bateman wrote:I know this is a pretty beginning Java question and if I knew what to call it, I could simply Google for the answer. If I have a class A that's going to be a "subset" of other classes B, C and probably others...

How do I code class B (and C, etc.) such that...

(Please answer the question in the comment above.)

Profuse thanks for taking pity on an old C programmer still too often struggling with the concepts.

Russ




How about declaring a int printMe variable in the super class. Then each subclass can set whatever value to printMe and resuse the super method?

Note - assuming you want to preserve x and not overwrite that.
11 years ago
That's OK; Polymorphism is a big thing in Java and offers some big advantages to the flexibility of your code at the price of trying to get your head round it to start with. I would suggest this fine example as an introduction. If you want a complete treatment see the java trail or I can highly recommend Head First Java which managed to make it fit in my head.

11 years ago
If the compiler allowed you to do that what would happen if you did this from a test harness in another package:



The Parent class declares a contract that it will execute a method called show with no arguments with the public modifier (so pretty much anything can call it). The Child class cannot break the contract made by the Parent only extend it; otherwise polymorphism dies.
11 years ago

Prasanna Raman wrote:
The error here "reference to varArgs is ambiguous". Why does the error show up here but not in the other code? Maybe because here both methods use varargs and in the other code, only one uses it?



I think you already answered your question. If you pass 3 ints which method would the complier use? You could argue that the second is "more specific" but it is a fine distinction. I'm sure the answer is in the JLS but I think the best approach is just to accept that overloading with varargs is to be avoided with the added bonus that you can also avoid reading the JLS.


More specifically, how does the compiler know if you are calling the first method with 3 int i varargs or the second method with int h and two int i varargs?
11 years ago
According to the JLS the compiler will choose the "most specific" method. There is also advice somewhere (can't find a reference to it now) not to overload with varargs for the very reason you are posting - it isn't always immediately clear which method will be called.
11 years ago

Aravindh Vin wrote:Similar problem.. What is the output of the below code and explain why?



Why don't you have a go first? You must be able to get the output as a minimum
11 years ago

Jay Mize wrote:Hey Phil, won't I have to force the while loop (randomLowUp > 90 && randomLowUp < 97) to start by initializing randomLowUp to a value like 92? Thanks



No, just initialize your random number by calling the random generator once prior to the loop. If the number meets your criteria then it you will never enter the loop.

11 years ago

Jay Mize wrote:Won't that create an array? Is there anyway to make Java generate a number between two set of range? Like generating a mumber from 65 to 90 or 97 to 122.



Why not generate a number between 65 and 122 and just reject it and run it again if it is between 90 and 97

Hint:


It's kind of inelegant and using an array of legitimate char values might be more simple in the end.
11 years ago

Jay Orsaw wrote:Hello all! I've been doing Java for awhile, but I've never really needed to use the args String, so I'm curious why would you?


I was reading this http://www.javaranch.com/drive/command.html

when I got into the Q, and they kind of spoke about it... It seems like it has to do with the command line though? Would we do args like that normally?



It is just a way to pass initial parameters to the main method when the program is called from the command line. I guess there might be other instances where you might use it but that's the only one I've used or can think of. What is it that you find odd? That you can only pass in String arrays?
11 years ago