
Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
I have no idea what the best way of going about coding this is. I've been stuck on it for a couple of hours now...
It has to be consecutive, either forwards or backwards, and has to loop. I think the String can be 4 digits max but I'm not entirely sure on that.
I've separated the String into chars but don't know what's the best way of separating letters from digits or best way of making them loop.
Thanks

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
pseudocode:
string s = "sddfa233"
loop over the string from index 1
compare current character with the one before it
if they are equal, you have a match.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
how do you compare it? how do you make sure its either only numbers or only letters? how do you make it so 'z' followed by an 'a' works, and 'a' followed by 'b' does as well? weve only learned if else and for statements, we havent learned arrays.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Are these the rules: if looking at 'a' then 'z' and 'b' are consecutive. For 'd', 'c' and 'e'. For '0', '9' and '1'

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Quote:
Originally Posted by
Norm
Are these the rules: if looking at 'a' then 'z' and 'b' are consecutive. For 'd', 'c' and 'e'. For '0', '9' and '1'
That is correct. And if you can, just guide me. I want to create the simplest way to solve this. The precious assignment had a similar question but instead it was using integers only, up or down, but not looping 09 or 90. For that I used % to separate the integer into ones, tens, hundreds and thousands and simply compared each digit (ones + 1) == tens etc.
Thanks

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
I assume the input is in a String. Use the charAt() method to get the char values out of the String.
Use a loop and if statements inside the loop to do the tests.
 Update 
char values in the az range are contiguous. For example: 'a' + 1 = 'b' and 'y' + 1 = 'z'

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
I have no clue how to test for both (+/1 or + /25 for letters, and +/1 or +/9 for numbers) using (charAt(i) == charAt(i + 1))

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
start simple: get the char before the current char and test it, then get the char after the current char and test it. Move to next char.
Don't try to do it all at once.
 Update 
Make a list of the steps the code needs to do in pseudo code to organize your thoughts on solving the problem. Rework the steps until they make logical sense as a solution. Then work on coding each step.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
can i do something like this? this code would be after I've found out the code is all letters and is ascending (I think I would need to find that out beforehand because for numbers the case needs to be +1 or +9). I also need something that figures out if its ascending or descending. Comparing the first and last variables wouldn't do this because it can loop xyzab. maybe I can try and compare them by estimating the difference between first and last variables assuming they're consecutive either way (up/down) and using the length of the String?
for i=0 to i<s.length() i++ {
if ((s.charAt(i) + 1) == s.charAt(i+1)  (s.charAt(i) + 25) == s.charAt(i+1)) {
lettercount +=
And so if lettercount == s.length() then that would prove the String has consecutive letters

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
When you look at the first char what is the first test you need to make?
Some thoughts:
To detect if ascending see if next is +1 in value
to detect descending see if next is 1 in value
Special tests for chars at end of sequences.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Hm. I think first it would be best to test for all numbers or all letters using match() (after turning to lowercase)
Then once I know that there are two options. For letters, I will check the first and last variables (s.charAt(0) and s.charAt(s.length()  1) ) and see if one +/1 or +/25 equals the other. For numbers, I will use +/1 and +/9. Then, based on what I find the relation between these last numbers is, I will continue to check for my known string of letters or numbers, known ascending or descending, for i=0 to i<s.length()  1, i++
Then I will proceed checking (one of the four cases will run, ascending letters, descending letters, ascending numbers or descending numbers). Once its done running I will know whether the remaining chars are ascending/descending the same way as the relationship between the first and last chars.
Is there any better way of doing this? Any problems?

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Quote:
check the first and last variables
Not very reliable for Strings more than 2 char long. I don't see how testing the first and ending char of the String wold tell you what was in between the first and last char.
Quote:
one of the four cases
How are you going to handle mixing the logic for digits and letters? What variables would you use to make the tests generic so the code wouldn't know if it was testing digits or letters? 'c' + 1 = 'd' and '2' + 1 = '3'

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
I'm not saying it would tell me what's in between. I'm saying it would narrow down my test. Without knowing if its ascending or descending then I'll have to test for either + or  which doesn't really make sense to me.
and for your second part, i don't see how I could make it generic when I have to consider 'a' and 'z' and '0' and '9' as consecutive. if i say charAt(i) + 9 == charAt(i+1) as one of the if statements, then if i have a string such as ajkl itll count it as consecutive. thats why i need to find out beforehand whether its all numbers or all letters.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Quote:
how I could make it generic
Define some variables to hold controlling values and use them instead of hardcoding: 'a', 'z', '0', '9' etc
You could determine ascending/descending by looking at the first two char

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Quote:
Originally Posted by
Norm
Define some variables to hold controlling values and use them instead of hardcoding: 'a', 'z', '0', '9' etc
You could determine ascending/descending by looking at the first two char
I'm not sure what you mean by the first sentence.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Instead of using 'a' in the code that tests for the first char of the group of letters: az or digits: 09, use a variable of type char that holds either 'a' or '0'

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Instead of using 'a' in the code?

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
How will you detect when testing the end of the sequence of letters? With this String: "bazyx" you will need to know when at the end of the sequence to know to wrap around to the end of the sequence: descending sequence going from 'a' to 'z'. Or with this: "89012" when going from '9' to '0'
All those values: 'a', 'z', '9', '0' should be in variables, not hardcoded.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
I'm really lost.
Code :
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
System.out.println("Enter 4 or less characters.");
Scanner keyboardReader = new Scanner(System.in);
String text = keyboardReader.next();
System.out.println(isConsecutive(text));
}
public static boolean isConsecutive (String s) {
int letterCount1;
int letterCount2;
int letterCount3;
int letterCount4;
String text = s.toLowerCase();
char one = 'a';
char two = 'z';
char three = '0';
char four = '9';
boolean consecutive = true;
boolean ascending;
boolean descending;
boolean azAscending;
boolean zaAscending;
boolean ascending09;
boolean ascending90;
if ((s.charAt(0)+1) == s.charAt(1)) {
ascending = true;
}
if ((s.charAt(0)1) == s.charAt(1)) {
descending = true;
}
if (s.charAt(0) == one && s.charAt(1) == two) {
ascending = true;
}
if (s.charAt(0) == two && s.charAt(1) == one) {
zaAscending = true;
}
if (s.charAt(0) == three && s.charAt(1) == four) {
ascending09 = true;
}
if (s.charAt(0) == four && s.charAt(1) == three) {
ascending90 = true;
}
for (i=1; i<s.length()1; i++) {
if (ascending = true && (s.charAt(i)+1) == s.charAt(i+1));
else {
for (int i=0; i<s.length(); i++) {
if ((s.charAt(i)+1) == s.charAt(i+1)
if (consecutive = true && (s.charAt(i) == one  s.charAt(i) == two  s.charAt(i) == three  s.charAt(i) == four)) {
consecutive = true;
}
}
return consecutive;
}
}
Some stuff is just things I left in case I decide to change my program around.

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Maybe this might give you some clues:
Code :
String str = "abdg";
char fred = str.charAt(0);
fred++;
char barney = str.charAt(1);
System.out.println(fred == barney);

Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')
Before trying to write code, you should work on the logic for the program. Work on making a list of the steps the program should do in pseudo code. When the logic looks good, then try writing the program.
In post #11 you said the first step was:
check if String is digits or letters
Then what?
The variable names should describe the data they hold. one, two etc don't say what they hold. Better would be beginLetters and endLetters, beginDigits and endDigits