Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Posts Tagged ‘Java

Java | ERROR: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

leave a comment »

ERROR: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

Story:
Ok, I hits the above error when I try to loop through the request parameters, and print out all parameter Key and Value.
The ClassCastException is a general exception, it will be thrown when you try to cast an Apple to an Orange. Thats simple!
When the first time I read this exception message, "[Ljava.lang.String; cannot be cast to java.lang.String", I though there is some typo in the compiler :P
What is the difference between "[Ljava.lang.String" and "java.lang.String"?
From the wording itself, quite straight forward. The later do not have "[L", but what is the "[L"?
Thanks for some stupid errors I hit before, I recall that the "[L" is the representation of Array object!

CODE / SOLUTION:
Here is the way I used to print out all parameter Key and Value. I'm not sure its a correct / best way, but its works!

Map paramMap = request.getParameterMap();
Iterator iter = paramMap.entrySet().iterator();
while(iter.hasNext()) {
    Entry<String, String[]> entry =  (Entry<String, String[]>) iter.next();
    String key = entry.getKey();
    String value = entry.getValue()[0];
    logger.debug("key[{}] value[{}]", key, value);
}

Written by Alfred

June 22, 2010 at 20:08

Posted in Java

Tagged with , , ,

Java | How to access Enum’s public method?

leave a comment »

Enum is an enumerated types, a special type in Java that used to hold static information.
Eg: the state of a connection (CONNECTING, CONNECTED, DISCONNECTED, etc), the day (MONDAY, TUESDAY, etc).
Besides of defining a status, we also can include some useful features of the Enum and write it as a method in the Enum.
Here is a simple example I write to show how to access to Enum’s method.

I have a Enum here, named “Day”:

package alfred.playground;

public enum Day {

	MONDAY, TUESDAY,
	WEDNESDAY, THURSDAY,
    FRIDAY, SATURDAY,
    SUNDAY;

	public boolean isWeekday() {
		return (equals(MONDAY) ||
				equals(TUESDAY) ||
				equals(WEDNESDAY) ||
				equals(THURSDAY) ||
				equals(FRIDAY));
	}

	public boolean isWeekend() {
		return (equals(SATURDAY) || equals(SUNDAY));
	}

}

And, here is the test program:

		Day day1 = Day.THURSDAY;
		System.out.println(String.format("[%s] is weekend? [%b]", day1, day1.isWeekend()));
		/*$>  [THURSDAY] is weekend? [false] */

		Day day2 = Day.SATURDAY;
		System.out.println(String.format("[%s] is weekend? [%b]", day2, day2.isWeekend()));
		/*$>  [SATURDAY] is weekend? [true] */

In order to call Enum’s method, we can not just directly call as usual class’s method “myClass.doSomething()”.
Instead, we must assign a value to the Enum before we can invoke the method.

For shorter codes, we can simplified the code by skipping the value assignment line:

		System.out.println(String.format("[%s] is weekend? [%b]", Day.FRIDAY, Day.FRIDAY.isWeekday()));
		/*$>  [FRIDAY] is weekday? [true] */

Written by Alfred

April 2, 2009 at 16:27

Posted in Java

Tagged with ,

Convert ArrayList to Array

leave a comment »

This is a basic Java conversion stuff. I want to convert an ArrayList to a normal Array.
Here is a sample I did and I would like to share it out to anyone who found its useful. ;)

The test driver…

.....
List arrayList_ = new ArrayList();
arrayList_.add("alpha");
arrayList_.add(new String("beta"));
arrayList_.add(new Integer(3));
arrayList_.add(new Float(4.00));
arrayList_.add(new Boolean(true));
.....
Object[] array_ = arrayListToArray(arrayList_);
for (int i=0; i<array_.length; i++) {
    System.out.println("[" + i + "] " + array_[i]);
}
// $>[0] alpha
// $>[1] beta
// $>[2] 3
// $>[3] 4.0
// $>[4] true
.....

The conversion happen here…

private Object[] arrayListToArray(List arrayList) {
    Object[] array = new Object[arrayList.size()];
    arrayList.toArray(array);
    return array;
}

Written by Alfred

December 30, 2008 at 18:11

Posted in Java

Tagged with , , ,

Follow

Get every new post delivered to your Inbox.