Friday, July 13, 2012

Playing with Pseudocode - Sequence of Numbers

If I have string of numbers like this "12345" for the input, and I want the output as a sequence of 3 numbers:
  1. 123
  2. 234
  3. 345
  4. 451
  5. 512
How to convert it to become Java Program?

First, let us make the pseudo-code before trying to code instead.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Loop start from `i` = 0 to `length`- 1
    1. Display sub-string of `n` from `i` to `i` + 3

And convert it to code.
  1.  public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;

  6.         for(i = 0; i < length; i++) {
  7.             System.out.println(n.substring(i, i + 3));
  8.         }
  9.     }
  10. }

 And let us check the output.
  1. 123
  2. 234
  3. 345
  4. Exception in thread "main" java.lang.StringIndexOutOfBoundException: String index out of range: 6
  5.     at java.lang.String.substring(String.java:1955)
  6.     at SequenceNumber.main(SequenceNumber.java:8)

 Wow, for the first time to code and we already make an exception, nice. Let's try figure out, in the line 8 of class Sequence Number the compiler said that "String index out of range: 6" that means when `i` = 3 it will get the sub-string of `n` from 3 to 6. If you try to display the `length` it will print 5, so the problem that make this exception is `i` + 3. Let's change the pseudo-code to make it avoid the exception.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Declare `x` as integer
  5. Loop start from `i` = 0 to `length` - 1
    1. Check if `i` + 3 > `length`
      1. Set `x` = length of sub-string of `n` from `i`
      2. Display sub-string of `n` from `i` concat with sub-string of `n` from 0 to 3 - `x`
    2. Else
      1. Display sub-string of `n` from `i` to `i` + 3

And make some update in the current code.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;
  6.         int x;
  7.        
  8.         for (i = 0; i < length; i++) {
  9.             if (i + 3 > length) {
  10.                 x = n.substring(i).length();
  11.                 System.out.println(n.substring(i) + n.substring(0, 3 - x));
  12.             } else {
  13.                 System.out.println(n.substring(i, i + 3));
  14.             }
  15.         }
  16.     }
  17. }

If you want to make it shorter in the code, try this one.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         char[] digits = Integer.toString(12345).toCharArray();
  4.         for (int i = 0; i < digits.length; i++) {
  5.             System.out.println("" + digits[i] + digits[(i + 1) % digits.length] + digits[(i + 2) % digits.length]);
  6.         }
  7.     }
  8. }
 Good luck...

References:
How can get the possible 3 digit combination from a number having more than 3 digits in java.

No comments:

Post a Comment