Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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'
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

    Quote Originally Posted by Norm View Post
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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 a-z range are contiguous. For example: 'a' + 1 = 'b' and 'y' + 1 = 'z'
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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))

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

    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.
    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'
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

    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
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

    Quote Originally Posted by Norm View Post
    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.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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: a-z or digits: 0-9, use a variable of type char that holds either 'a' or '0'
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need to find out if a String has consecutive letters or digits with LOOPING ('89012' or 'bazyx')

    I'm really lost.
    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.
    Last edited by Norm; October 1st, 2013 at 06:54 AM. Reason: quote tags changed to code

  20. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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:
    String str = "abdg";
    char fred = str.charAt(0);
    fred++;
    char barney = str.charAt(1);
    System.out.println(fred == barney);
    Improving the world one idiot at a time!

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Separating Digits in a String
    By Staticity in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 06:39 AM
  2. Replies: 1
    Last Post: September 17th, 2011, 10:28 AM
  3. Problem with digits/letters
    By te09hn8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2011, 07:30 PM
  4. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  5. Replies: 1
    Last Post: December 2nd, 2010, 05:29 PM