-2

I have an array int[] myArray = new int[] { A, B, C, D, E }; The a,b,c,d,e are randomly set to the values of 6,4,5,1,1 from previous code.

I need to find a way to look for: IF array contains a 6, 5, or 4. I have to have a 6 first, then a 5, and then a 4. If the array contains 6,4,1,2,1, then I only need to find the 6. I cant use the 4 without a 5. If the array is set to 6,1,2,1,5, then I need the 6 and 5.

I need to put the "found" numbers (6,5,4) in the array into variables and print them to the console.

Thoughts, ideas, tutorials that might help?

Thank you

RazorSharp
  • 179
  • 1
  • 3
  • 15

1 Answers1

1
public class Filterable<T> {

   private List<T> source

   public Filterable(List<T> aSource) { source = aSource; }

   public List<T> filter( Filter<T> aFilter ) {
       List<T> dest = new ArrayList<T>();
       for( T current : source ) {
           if( filter.contains(current) ) {
              dest.add( current );
           }
       }
       return dest;
   } 

   public static void main(String[] args) {
      String message = "V ubcr guvf vfa'g ubzrjbex orpnhfr ";
      message += "V tbg vg bss fgnpxbiresybj: ";
      message += "uggc://fgnpxbiresybj.pbz/dhrfgvbaf/21664702/wnin-frnepu-na-vagrtre-neenl-sbe-yvfg-bs-enaqbz-inyhrf";

      Filterable f1 = new Filterable( Arrays.asList( 6, 4, 1, 1, 2 ) );
      Filterable f2 = new Filterable( Arrays.asList( 6, 3, 1, 5, 4 ) );
      Filterable f3 = new Filterable( Arrays.asList( 6, 4, 1, 6, 5, 4 ) );

      List<Integer> r1 = f1.filter( new OrderedFilter( 6, 5, 4 ) );
      List<Integer> r2 = f2.filter( new OrderedFilter( 6, 5, 4 ) );
      List<Integer> r3 = f3.filter( new OrderedFilter( 6, 5, 4 ) );

      print( r1 );
      print( r2 );
      print( r3 );
      System.out.println(rot13(message));
   }

   public static void print(List<Object> r1) {
      boolean first = true;
      for( Object o : r1 ) {
         if( !first ) System.out.print(", ");
         System.out.printf( "%s", o );
         first = false;
      }
      System.out.println();
   }

   public static String rot13(String input) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < input.length(); i++) {
         char c = input.charAt(i);
         if       (c >= 'a' && c <= 'm') c += 13;
         else if  (c >= 'A' && c <= 'M') c += 13;
         else if  (c >= 'n' && c <= 'z') c -= 13;
         else if  (c >= 'N' && c <= 'Z') c -= 13;
         sb.append(c);
      }
      return sb.toString();
   }
}

public interface Filter<T> {
   public boolean contains(T object);
}

public OrderedFilter implements Filter<T> {
   private T[] lookForThese;
   private int currentMatch = 0;

   public OrderedFilter( T... values ) {
      lookForThese = values;
   }

   public boolean contains(T current) {
      if( currentMatch >= lookForThese.length ) return false;
      if( lookForThese[currentMatch].equals( current ) ) {
         currentMatch++;
         return true;
      }
      return false;
   }
}
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Wow... that's a lot of detail. I cant see how this is reading from the array. My array is populated using a loop repeated 5 times with Random rand = new Random(); int a = (int) (6 * rand.nextDouble() + 1); If I use this logic, I cant predict what any one run will produce. I just need to read the array to find whether array contains a 6,5, or 4. Thank you – RazorSharp Feb 09 '14 at 21:51
  • Your question specified needing the numbers found out of the original array in some predefined order so it can be printed. Therefore, that's what you got. Your comment says you need something entirely different. Anyway, start with the main method as just an example of how to use Filterable class. The loop is in the filter() method. It's 3 classes. You could just run it if you don't fully understand it. – chubbsondubs Feb 10 '14 at 02:11