11 Replies - 252 Views - Last Post: 11 May 2013 - 02:04 PM Rate Topic: -----

#1 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Calculation of delay in time

Posted 10 May 2013 - 08:10 PM

Maybe it sounds too easy but im really confused..
for example

public void calculationOfDelay() {

       Calendar cal = Calendar.getInstance();
       cal.getTime();

       SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
       String  time = s.format(cal.getTime());

       sendTime(time);
       Thread.sleep(2000) //delay      
}

public void getTime() {

       String time = sendTime();
       // now how to calculate the delay and set the time correctly?
}




This post has been edited by psh: 10 May 2013 - 08:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculation of delay in time

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Calculation of delay in time

Posted 10 May 2013 - 08:20 PM

And we should invent the question ?
What is your question ?
What is sendTime(); ? You haven't post that method

public void calculationOfDelay() {

       Calendar cal = Calendar.getInstance();
       cal.getTime();   <--- this line is completly useless you don;t save returned value

       SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
       String  time = s.format(cal.getTime());

       sendTime(time);               <--- sendTime() wuith a String as parameter
       Thread.sleep(2000)    <--- that can;t work without a try.catch clause      
}

public void getTime() {

       String time = sendTime();    <--- another sendTime() with no parameter
}



Was This Post Helpful? 0
  • +
  • -

#3 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 01:11 AM

View Postpbl, on 11 May 2013 - 03:20 AM, said:

And we should invent the question ?
What is your question ?
What is sendTime(); ? You haven't post that method

public void calculationOfDelay() {

       Calendar cal = Calendar.getInstance();
       cal.getTime();   <--- this line is completly useless you don;t save returned value

       SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
       String  time = s.format(cal.getTime());

       sendTime(time);               <--- sendTime() wuith a String as parameter
       Thread.sleep(2000)    <--- that can;t work without a try.catch clause      
}

public void getTime() {

       String time = sendTime();    <--- another sendTime() with no parameter
}




HAHAHA im really sorry i dont sleep well these days



public void sendPacket() throws InterruptedException {
        
         // send that 5 times for more acurate results
         for(int i = 0; i < 5; i++)    
         { 
              Calendar cal = Calendar.getInstance();
    	 
    	      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    	      String  time =sdf.format(cal.getTime());
              
              // writer is Printwriter which is writing in the OutputStream
              writer.write(time+"\n"); 
              writer.flush(); 
              
              // this is the delay for each time that sends the time
              Thread.sleep(2000);
         }
    }

  public void getPacket() {
        
        String answer = null;
        // reader BufferedReader which reads InputStream
        try 
        {
            while ((response = reader.readLine()) != null) 
            {
                String time = answer;
                // do calculation to find the delay
                // in the time received , correct it and then 
                // print it and break

                System.out.println(time);
                break;
            }
        } catch (IOException ex) {
        }
    }




This post has been edited by psh: 11 May 2013 - 01:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Calculation of delay in time

Posted 11 May 2013 - 01:19 AM

Strike 2. What's the question? What confuses you?
Was This Post Helpful? 0
  • +
  • -

#5 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 01:25 AM

View PostGregBrannon, on 11 May 2013 - 08:19 AM, said:

Strike 2. What's the question? What confuses you?



Hello..my problem is how to calculate the delay in the second method getPacket() and set the time correctly

Method sendPacket writes to OutputStream
Method getPacket reads from InputStream

This post has been edited by psh: 11 May 2013 - 01:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 02:39 AM

What im doing wrong?i want to calculate the average in time

        public void findAvgTime() {
        String [] randomTimes= { "12:00:00","11:00:00"};
        
        int totalHours = 0 ;
        int totalMin = 0 ;
        int totalSeconds = 0 ;
        
        int counter = 0;
        
        int avgH = 0;
        int avgM = 0;
        int avgS = 0;
        
        for(int i = 0; i < randomTimes.length; i++)
        {
           
            String []array = randomTimes[i].split(":");
            
            int hours   = Integer.parseInt(array[0]);
            int minute  = Integer.parseInt(array[1]);
            int seconds = Integer.parseInt(array[2]);
            
            totalHours   = totalHours   + hours;
            totalMin     = totalMin     + minute;
            totalSeconds = totalSeconds + seconds;
            
            counter++;
            
            avgH = totalHours    / counter;
            avgM = totalMin      / counter;
            avgS = totalSeconds  / counter;    
            
            System.out.println(totalHours);
            
        }
        System.out.println(avgH      +":"+
                           avgM      +":"+
                           avgS );
           
       }



Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Calculation of delay in time

Posted 11 May 2013 - 02:50 AM

You should explain what your code is doing, and what it should be doing, and post any errors that you receive.

However, you have declared everything as integers, so the average of 11 and 12 will be 11, not 11.5.

This post has been edited by andrewsw: 11 May 2013 - 02:51 AM

Was This Post Helpful? 2
  • +
  • -

#8 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 02:58 AM

View Postandrewsw, on 11 May 2013 - 09:50 AM, said:

You should explain what your code is doing, and what it should be doing, and post any errors that you receive.

However, you have declared everything as integers, so the average of 11 and 12 will be 11, not 11.5.


Damn im stupid trying to find avg with int ..thanks man
i changed to double

My code should find the average between random times that i have in the array randomTimes[]
..i got the right result but is not in really good format .. 11.5:0.0:0.0 any ideas
how to make calculation of average time better?
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Calculation of delay in time

Posted 11 May 2013 - 03:08 AM

You don't have to quote the previous poster. It's there already for everyone to see.

As for improving the output, you'll have to strip any decimal parts and convert to the next time division. 11.5 hours = 11 hours, 30 minutes, 00 seconds.
Was This Post Helpful? 1
  • +
  • -

#10 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 03:17 AM

any help with that..i have no clue :dontgetit:
Was This Post Helpful? -2
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Calculation of delay in time

Posted 11 May 2013 - 01:36 PM

Duplicate threads merged. Please avoid duplicate posting.

Quote

any help with that..i have no clue :dontgetit:

Really? There are 60 seconds in a minute. I think you can figure out how to convert 11.5 minutes to minutes and seconds.
Was This Post Helpful? 1
  • +
  • -

#12 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Calculation of delay in time

Posted 11 May 2013 - 02:04 PM

View Postmacosxnerd101, on 11 May 2013 - 08:36 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

Quote

any help with that..i have no clue :dontgetit:/>

Really? There are 60 seconds in a minute. I think you can figure out how to convert 11.5 minutes to minutes and seconds.



its not the same topic.one is about to find delay in time and the second is about to find the average in time..thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1