Browsing 7239 questions and answers with Jon Skeet

Java set transparency on Color.color WITHOUT using rgb's

I'm woundering, is there a way to set transparency on a color without using rgb's. For example you can do this Color color = new Color(255,255,255,Transparency) but I want to...
Jon Skeet
people
quotationmark

It doesn't look like there's any such method or constructor at the moment, but it's dead easy to write a helper method: public static Color newColorWithAlpha(Color original, int alpha) { return new Color(color.getRed(),... more 3/31/2015 10:12:39 PM

people

Date Manipulation using Calendar class in Java

Please refer to the simple code below. I want to deduct one month from current month and expecting value of February 2015 in output. Calendar today = Calendar.getInstance(); ...
Jon Skeet
people
quotationmark

You're setting the month number to be -1, i.e. "December of the year before". You want to add -1 month: today.add(Calendar.MONTH, -1); I'd strongly recommend using Joda Time or Java 8's java.time package if you possibly can though -... more 3/31/2015 3:56:47 PM

people

GetMethod and invoke it via Reflection passing Object[] as param

I have the following code snippet which throws exception, the problem seem to be with the parameter which I'm trying to pass to 'foo' method. public void test() { try { ...
Jon Skeet
people
quotationmark

The Method.invoke method takes an Object[] parameter which corresponds to the arguments you want to provide. As you've got a single parameter which is of type Object[], you need to wrap that in another Object[]. For... more 3/31/2015 3:15:47 PM

people

java.lang.IllegalMonitorStateException on timeout

Is it possible to get java.lang.IllegalMonitorStateException on notifyAll() because wait(timeout) has passed the timeout time? If not, what happens when notifyAll() occurs after...
Jon Skeet
people
quotationmark

No, you won't get an exception for that. If you call notifyAll when nothing is waiting, nothing happens basically. It's not an error condition. more 3/31/2015 11:22:14 AM

people

Using indexes to get the sum of all the elements in an array list gives unexpected output. What's wrong with my code?

I have a folder that contains four files. Each of the four files contains an integer. My code reads the integers from the files into an ArrayList. // Reading the save...
Jon Skeet
people
quotationmark

You've declared sum inside your loop - so you're starting again on each iteration. You want: int sum = 0; for(int i = 0; i < al.size(); i++) { sum += al.get(i); } System.out.println("The sum is: " + sum); Note that: I've changed... more 3/30/2015 1:45:14 PM

people

How do I spawn 10 threads to process a Dictionary?

I have a dictionary of usernames, I want to check whether these users can be used for login into a website, but iterating it one by one would last long since it's a huge list, I...
Jon Skeet
people
quotationmark

Firstly, if you're looking to use lots of connections to a single site, you may well want to tweak the process configuration to allow that - otherwise you'll find you get a bottleneck on the WebClient. See the <connectionManagement>... more 3/30/2015 1:18:13 PM

people

Why java Scanner is not reading all the lines?

Here is my code which just reads the lines from input stream and displays them but to my surprise its not reading all the lines. Its reading only upto second last line. Here is ...
Jon Skeet
people
quotationmark

The problem is that the first call to nextLine() is reading the empty "line" between the end of "5" (read for m) and the line break. Personally, I would either stop using Scanner entirely (in favour of BufferedReader) - there's a huge... more 3/30/2015 11:34:46 AM

people

synchronized keyword and locks on instance methods

I'm studying concurrency at the moment and I was learning about blocking threads. I know that a thread can come to a blocked state when the corresponding attached task tries to...
Jon Skeet
people
quotationmark

even if the syn.manipulate() method is locked so I'm assuming the task cannot get to calling getI() That's the mistake you're making, I believe. Just because one method is synchronized doesn't implicitly mean that anything else is... more 3/30/2015 11:25:35 AM

people

Member, class, accessibility clarification

Here is an example that I can't understand: abstract class Personne { protected static int nbPersonnes=0; static void nbpersonnes (){ System.out.println( “\n...
Jon Skeet
people
quotationmark

i thought class Secretaire can access Personne'members, and not the opposite ? That would be true in C#, but not in Java. In Java, all the code within the same top-level class can access all the private members of all the classes... more 3/29/2015 1:22:13 PM

people

HashSet is empty

Why is my HashSet<Circle> empty? LatLng currentLocation = new LatLng(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude()); GoogleMap googleMap =...
Jon Skeet
people
quotationmark

You're creating a new - and empty - set each time the code runs: HashSet<Circle> circles = new HashSet<>(); if (!circles.isEmpty()) { // You'll never get in here... } If you want to maintain the same set across calls,... more 3/28/2015 10:03:02 AM

people