Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I created this simple example which is used to read Linux uptime:

public String getMachineUptime() throws IOException {
    String[] dic = readData().split(" ");

    long s = (long) Array.get(dic, 1);
    return calculateTime(s);
}

private String readData() throws IOException {
    byte[] fileBytes;
    File myFile = new File("/proc/uptime");
    if (myFile.exists()) {
        try {
            fileBytes = Files.readAllBytes(myFile.toPath());
        } catch (java.nio.file.AccessDeniedException e) {
            return null;
        }

        if (fileBytes.length > 0) {
            return new String(fileBytes);
        }
    }
    return null;
}

private String calculateTime(long seconds) {
    int day = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours = TimeUnit.SECONDS.toHours(seconds)
            - TimeUnit.DAYS.toHours(day);
    long minute = TimeUnit.SECONDS.toMinutes(seconds)
            - TimeUnit.DAYS.toMinutes(day)
            - TimeUnit.HOURS.toMinutes(hours);
    long second = TimeUnit.SECONDS.toSeconds(seconds)
            - TimeUnit.DAYS.toSeconds(day)
            - TimeUnit.HOURS.toSeconds(hours)
            - TimeUnit.MINUTES.toSeconds(minute);

    return "Day " + day + " Hour " + hours + " Minute " + minute
            + " Seconds " + second;
}

When I run the code I get this exception:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long

Is there any other way to convert the result?

share|improve this question
    
this line .long s = (long) Array.get(dic, 1); –  Fast Snail Oct 13 '14 at 10:57
    
Can you help me to fix this? –  user1285928 Oct 13 '14 at 10:58
    
you got the answer –  Fast Snail Oct 13 '14 at 10:59

2 Answers 2

I believe you have to replace

long s = (long) Array.get(dic, 1);

with

long s = Long.valueOf((String) Array.get(dic, 1));

or even better:

long s = Long.valueOf(dic[1]);

The reason is that your array consists of String object, and direct casting won't work.

share|improve this answer
    
I get this no suitable method found for valueOf(Object) method Long.valueOf(String) is not applicable (argument mismatch; Object cannot be converted to String) method Long.valueOf(long) is not applicable (argument mismatch; Object cannot be converted to long) –  user1285928 Oct 13 '14 at 10:59
    
See my updated answer –  kocko Oct 13 '14 at 11:01
    
Now I get this message java.lang.NumberFormatException: For input string: "634618.11 –  user1285928 Oct 13 '14 at 11:01
1  
Well...this means it's not long but double. :) –  kocko Oct 13 '14 at 11:02

The problem appears to be in the following line:

long s = (long) Array.get(dic, 1);
  1. The get(Object array, int index) method of java.lang.reflect.Array returns an instance of Object, which cannot be directly cast to long.

  2. You can access the element of the array simply by dic[1] instead of Array.get(dic, 1)

Replace with the following code:

long s = Long.parseLong(dic[1]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.