Comparing three variables with another three variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geforceter
    New Member
    • Jul 2007
    • 2

    Comparing three variables with another three variables

    I am doing a Java ME project which acquires input such as current location and destination from the use and then display the appropriate information such as bus service to take, travel time and image of the bus stop.

    So here the problem...

    There are three bus services for each location.
    Location 1: 95, 100, 14
    Location 1: 20, 14, 144 where 20 95 100 and 14 are the bus number.

    i have retrieved the bus services from mysql.
    they are stored as String types
    cbus1,cbus2,cbu s3 (services for current location) and
    dbus1, dbus2, dbus3 (services for destination).

    I need to run through them get the services that are present in both the current location and destination.

    I have initialized three variables common1, common2, common3 to 0.

    I need to store the value of the common services in these three variables.

    After they are stored I will use a very simple but long if else statement to combine them as a variable and send to the midlet.
    Code:
    if (common1==0&&common2==0 &&common3==0){
    String bussvc = "No Services available";
    }
    else if (common2==0 &&common3==0){
    String bussvc = Integer.toString(common1);
    }
    else if (common1==0 && common3==0){
    String bussvc = Integer.toString(common2);
    }
    else if (common1==0 && common2==0){
    String bussvc = Integer.toString(common3);
    }
    else if (common1==0 && common2==0){
    String bussvc = Integer.toString(common3);
    }
    else if (common3==0){
    String bussvc = ""+Integer.toString(common1)+"+","+"+Integer.toString(common1)+""
    }
    
    .
    .
    .
    .
    .
    How do cycle through the bus numbers and get the common ones?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have you looked at a Set or even a BitSet?

    kind regards,

    Jos

    Comment

    • geforceter
      New Member
      • Jul 2007
      • 2

      #3
      How do I use a BITSET to compare the values and store the common ones ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by geforceter
        How do I use a BITSET to compare the values and store the common ones ?
        Given your example:

        Location 1: 95, 100, 14
        Location 1: 20, 14, 144

        build two bitsets; the first bitset has bits 95, 100 and 14 set; the second one
        has bits 20, 14 and 144 set. 'and' the two sets and your're in business.
        Read the API docs for the BitSet class and see how it can be done.

        kind regards,

        Jos

        Comment

        Working...