0

I have developed the java ping client application.I am struct at adding the elements to the list and converting them to array in the Deviation class deviation method.

My Pingjava class :

public class PingClass{
     public static void main(String args[]) 
              throws IOException
              {
                // create the ping command as a list of strings
         PingClass ping = new PingClass()
          public void doCommand(List<String> command) 
          throws IOException
          {
            String s = null;
            String Serror=null;

            ProcessBuilder pb = new ProcessBuilder(command);
            Process process = pb.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null)
            {
             //logic here
              }

          }

} I need to catch the TTL values and send them to the Deviation class.i am struct here in adding the TTL values to the above list from the above screen.I donot know whether it is a better approach or not ?

In the Deviation class:

public class Deviation {
    // Beginning of method 
    public static double deviation(List<Integer> t2) {   
//I am getting problem here although i have typecasted to double
double[] array = new double[t2.size()];
double[] numbers= double[](t2.toArray());

    } // End of double findDeviation    
} // End of class Deviation

I am getting typecast problem of converting of list type to array in the Deviation class.I am getting error at this line. double[] numbers= double; Please assist me in this.

1 Answer 1

0

I haven't actually tested this out but I believe toArray() converts a List to Object[]

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray()

Which would mean that the following

t2.toArray()

converts a List<Integer> to a Integer[].

Because a primitive double[] and a object Integer[] are completely different, I would say the best solution is probably to convert your List to double[] prior to passing to your function

Edited

This is how I would do this

I would add values to a list outside the while loop since taking standard deviation on a single object doesn't make any sense:

List<Double> doubleList = new ArrayList<Double>();            

while ((s = stdInput.readLine()) != null)  {
    System.out.println(s);
    if(s.contains("TTL")) {
        // Get value as a double
        Double val = Double.parseDouble(s.substring(44, 47));
        // Add to doubleList
        doubleList.add(val);
    }
}

// Call deviation
Deviation.deviation(doubleList)

Afterwards, I would change the parameter of the deviation class method to

public static double deviation(List<Double> t2)

and then check the output.

If you still get a problem, then I would refer to the following link to learn about changing List objects to primitive arrays

How to cast from List<Double> to double[] in Java?

4
  • I want to bind the all the TTL values show diagram and send that list to Deviation class and there i want to convert the same to array(i am getting error with the typecasting). Could you please help me in correcting the below code. if(s.contains("TTL")) { List<Integer> t1 = new ArrayList<Integer>(); t1.add(Integer.parseInt(s.substring(44, 47))); System.out.println(t1); for (Integer number : t1) { System.out.println("Number = " + number); }
    – chandu
    Mar 27, 2016 at 5:07
  • Thanks alot Sir It helped alot :)
    – chandu
    Mar 27, 2016 at 17:59
  • You can accept the answer so that others running into a similar problem know where to look at! Good Luck on your app=)
    – jrhee17
    Mar 28, 2016 at 4:08
  • I accepted the answer.Thanks alot sir.
    – chandu
    Apr 3, 2016 at 18:23

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