I am stuck in the following problem from a long time -

I want to create a String array StrTime from 3 Int arrays.

Input Arrays

int Time1[]={"1.1","2.1","4.1"};
int Time2[]={"2.5","4.7","5.1"};
int Time3[]={"12.55","1.11","2.1"};

CurrentTime = 2.0;

So loop though each integer array, check Time1, the first array item which is greater than 2.0 should be put in our String array, Noe check Time2 for the same and Time3.

At end we should get output String array as -

Output Array

String strTime[] ={"2.1","None","2.1"}

Can someone help me????

UPDATE:

Also is there a way to create a double [] like below-\ {1,2,,3,4,,5}

share|improve this question

25% accept rate
3  
that should not compile. You are assigning int[] a String[]!!! – Nishant Aug 11 at 13:13
The array initialization won't compile and - shouldn't the output be {"2.1", "2.5", "12.55"} if I get your requirements right... – Andreas_D Aug 11 at 13:14
@Nishant. Yes, because of that fact, I would also call it DIFFICULT – Alexander Pogrebnyak Aug 11 at 13:14
@AlexanderPogrebnyak lol – Nishant Aug 11 at 13:15
What have you tried? – Tony Ennis Aug 11 at 13:15
feedback

3 Answers

up vote 0 down vote accepted

May be a typo, but that's the way to init the arrays (they should be double):

double time1[]={1.1, 2.1, 4.1};
double time2[]={2.5, 4.7, 5.1};
double time3[]={12.55, 1.11, 2.1};

Hints:

  • create a new String array for the result
  • for-loop through the first array
  • compare the current value with your threshold, if the current value is greater then store it in the first slot of the string array (need to convert it to string), and break out of the loop.
  • repeat the last to steps for the other two arrays
share|improve this answer
I was still editing it... – Andreas_D Aug 11 at 13:18
How to convert that to String. – user1560909 Aug 11 at 14:08
String intAsString = "" + intValue; - this is the simplest solution for this problem. – Andreas_D Aug 11 at 14:23
BTW - why did you add that update to your question? The answer to that is in my and in Reimus' answers.. Haven't you read it? – Andreas_D Aug 11 at 14:24
feedback

Use:

    double time1[]={1.1, 2.1, 4.1};
    double time2[]={2.5, 4.7, 5.1};
    double time3[]={12.55, 1.11, 2.1};
share|improve this answer
feedback

When you get it compiling, you could use org.apache.commons.collections.CollectionUtils and org.apache.commons.collection.Predicate to filter without loops.

CollectionUtils.find( getACollectionOfYourArray() , new Predicate() {
            public boolean evaluate(Object val) {
                // if is gt 2
                return true;
            }

        });
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
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.