Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 20th, 2013  |  syntax: Java  |  size: 3.31 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2.  
  3. public class Question1 {
  4.  public static void main(String[] args) {
  5.   Scanner reader = new Scanner(System.in);
  6.   System.out.println("What would you like me to check for consecutiveness?");
  7.   String message = reader.nextLine();
  8.   if ((consecIncrease (message) == true) || (consecDecrease (message)== true)) {
  9.    System.out.format("The message has consecutive characters.");
  10.   }
  11.   else
  12.   {
  13.    System.out.format("The message does not have consecutive characters.");
  14.   }
  15.  
  16.  } // end main
  17.  
  18.  // method 1
  19.  public static boolean consecIncrease(String message) {
  20.   boolean lowerCaseConsec = false;
  21.   boolean upperCaseConsec = false;
  22.   boolean numberConsec = false;
  23.   boolean result = false;
  24.  
  25.   //should check one less than the message length.
  26.   for (int i = 0; i < message.length() - 1; i++) {
  27.    if ((message.charAt(i) >= 'a' && message.charAt(i) <= 'z')
  28.       && (((message.charAt(i) == message.charAt(i + 1)+ ('a' - 'b'))
  29.       || (message.charAt(i) == message.charAt(i + 1) + ('a' - 'B')))
  30.       || (message.charAt(i+1)>= '0' && message.charAt(i+1)<= '9'))) {
  31.     lowerCaseConsec = true;
  32.    }
  33.    else if ((message.charAt(i) >= 'A' && message.charAt(i) <= 'Z')
  34.            && (((message.charAt(i) == message.charAt(i + 1)+ ('A' - 'B'))
  35.            || (message.charAt(i) == message.charAt(i + 1) + ('A' - 'b')))
  36.            || (message.charAt(i+1)>= '0' && message.charAt(i+1)<= '9'))) {
  37.     upperCaseConsec = true;
  38.    }
  39.    else if ((message.charAt(i) >= '0' && message.charAt(i) <= '9')
  40.            && (((message.charAt(i) == message.charAt(i + 1)+ ('0' - '1')))
  41.            || (message.charAt(i+1) >= 'a' && message.charAt(i+1) <= 'z')
  42.            || (message.charAt(i+1)>= 'A' && message.charAt(i+1)<= 'Z'))) {
  43.     numberConsec = true;
  44.    }
  45.    else {
  46.    result = false;
  47.    }
  48.   }// end for
  49.   boolean finalResult = (lowerCaseConsec && upperCaseConsec && numberConsec && result);
  50.   return finalResult;
  51.  } // end method 1
  52.  
  53.  
  54.  // method 2
  55.  public static boolean consecDecrease(String message) {
  56.   boolean lowerCaseConsec = false;
  57.   boolean upperCaseConsec = false;
  58.   boolean numberConsec = false;
  59.   boolean result = false;
  60.  
  61.   for (int x = 0; x < message.length() - 1; x++ ) {
  62.    if ((message.charAt(x) >= 'a' && message.charAt(x) <= 'z')
  63.       && (((message.charAt(x) == message.charAt(x+1) +('b' - 'a'))
  64.       || (message.charAt(x) == message.charAt(x+1) + ('b' -'A')))
  65.       || (message.charAt(x+1)>= '0' && message.charAt(x+1)<= '9'))) {
  66.    lowerCaseConsec = true;
  67.    }
  68.    else if ((message.charAt(x) >= 'A' && message.charAt(x) <= 'Z')
  69.           && (((message.charAt(x) == message.charAt(x+1) + ('B' - 'A'))
  70.           || (message.charAt(x) == message.charAt(x+1) + ('B' -'a')))
  71.           || (message.charAt(x+1)>= '0' && message.charAt(x+1)<= '9'))) {
  72.    upperCaseConsec = true;
  73.    }
  74.    else if ((message.charAt(x) >= '0' && message.charAt(x) <= '9')
  75.            && (((message.charAt(x) == message.charAt(x+1) + ('2' - '1')))
  76.            || (message.charAt(x+1) >= 'a' && message.charAt(x+1) <= 'z')
  77.            || (message.charAt(x+1)>= 'A' && message.charAt(x+1)<= 'Z'))) {
  78.     numberConsec = true;
  79.    }
  80.    else {
  81.     result = false;
  82.    }
  83.   }// end for
  84.   boolean finalResult = (lowerCaseConsec && upperCaseConsec && numberConsec && result);
  85.   return finalResult;
  86.  }//end method 2
  87. }