Skip to content

Instantly share code, notes, and snippets.

Created October 21, 2013 00:51
Show Gist options
  • Save anonymous/7077185 to your computer and use it in GitHub Desktop.
Save anonymous/7077185 to your computer and use it in GitHub Desktop.
public class Question1{
public static void main(String[]args){
String sample = "257";
//Calling the method isConsecutive
isConsecutive(sample);
System.out.print(isConsecutive(sample));
}
//this is our method to see if the value inputted is consecutive or not conscutive
//it calls on the other boolean methods
public static boolean isConsecutive(String s){
//calling the methods from below
if(isConsecutiveAscending(s)){
return true;
}
if(isConsecutiveDescending(s)){
return true;
}
return false;
}
//this is the method to evaluate consecutiveness going right (ascending)
public static boolean isConsecutiveAscending(String s){
//need to make the string in all lower cases (since not differentiating
//between upper case and lower case)
s = s.toLowerCase();
for(int i = 0; i<(s.length()-1);i++){
boolean trueAscending=true;
if((int)(s.charAt(i)+1) == (int)s.charAt(i+1)){
trueAscending = true;
}
//FOR SOME REASON THIS PART JUST ISN'T WORKING
//need to evaluate the special case going from end to beginning (9->1)
else if((((int)s.charAt(i))==9)&&(((int)s.charAt(i+1))==0)){
trueAscending = true;
}
//need to evaluate special case from end to beginning for letter (z->a)
else if(((int)s.charAt(i)==122)&&((int)s.charAt(i+1)==97)){
trueAscending = true;
}
else{
return false;
}
}
return true;
}
//this is the method to evaluate consecutiveness going left (descending)
public static boolean isConsecutiveDescending(String s){
boolean trueDescending = true;
s = s.toLowerCase();
for(int i = 0; i<(s.length()-1);i++){
if((int)(s.charAt(i)-1)==(int)s.charAt(i+1)){
trueDescending = true;
}
//need to evaluate the boundary case
else if(((int)s.charAt(i)==0)&&((int)s.charAt(i+1)==9)){
trueDescending = true;
}
else{
trueDescending = false;
}
}
return trueDescending;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment