Counting Characters and Permutations of Arrays

I just finished the “Basic” Java exercises at w3resource.com today and made the last two — #38 and #39 — a bit harder than they needed to be. #38 asks for you to “write a Java program to count the letters, spaces, numbers and other characters of an input string”. I interpreted this to mean each individual character, for instance, how many “h” or “L” or whatever. The solution suggests the use of Character.isLetter(), Character.isDigit(), and Character.isSpaceChar(), but I like my way better anyway:

import java.util.Scanner;

public class Exercise38 {
  public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a line of text: ");
    String userLine = in.nextLine();

    char[] userChars = userLine.toCharArray();
    int len = userLine.length();
    int[] charFrequency = new int[len];

    boolean counted = false;

    for(int ii = 0; ii < len; ++ii){
      for(int jj = 0; jj < ii; ++jj){
        counted = false;
        if(userChars[ii]==userChars[jj]){
          charFrequency[jj]++;
          counted = true;
          break;
        }
      }
      if(!counted){
        charFrequency[ii]++;
      }
    }

    System.out.format("In your string: \n\t\"%s\"\nthere were...\n", userLine);

    for(int ii = 0; ii < len; ++ii){       if(charFrequency[ii] > 0){
        System.out.format("\t%d \'%c\' character(s)\n", charFrequency[ii], userChars[ii]);
      }
    }

    return;
  }
}

This above code will count each individual character in a user-inputted String. Below is some sample output:

15:14 |aww@airy Basic| javac Exercise38.java; java Exercise38
Enter a line of text:
This is a line of text
In your string:
	"This is a line of text"
there were...
	1 'T' character(s)
	1 'h' character(s)
	3 'i' character(s)
	2 's' character(s)
	5 ' ' character(s)
	1 'a' character(s)
	1 'l' character(s)
	1 'n' character(s)
	2 'e' character(s)
	1 'o' character(s)
	1 'f' character(s)
	2 't' character(s)
	1 'x' character(s)

The second Exercise with instructions which I misinterpreted was Exercise 39. This one asks you to “write a Java program to create and display unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there.” I assumed that this wasn’t meant to be hard-coded. That any numbers should be able to be used. I also expanded the code to allow arrays of any length. In essence, what I wrote was a Java program which gives all of the permutations of an array of numbers:

public class Exercise39 {
  public static void main(String[] args){

    int[] testArray = { 7, 3, 2 };
    printArray(permuteIntegerArray(testArray));

    return;
  }

  public static void printArray(int[][] printMe){
    int nRows = printMe.length;
    int nCols = printMe[0].length;

    System.out.print("\n");
    for(int ii = 0; ii < nRows; ++ii){
      System.out.format("%d\t : ", ii+1);
      for(int jj = 0; jj < nCols; ++jj){
        System.out.print(printMe[ii][jj] + " ");
      }
      System.out.print("\n");
    }
  }

  public static int[][] permuteIntegerArray(int[] array){
    int n = array.length;
    int nFactorial = integerFactorial(n);
    int nMinus1Factorial = integerFactorial(n-1);
    int[][] permuted = new int[nFactorial][n];

    if(n==1){
      permuted[0][0] = array[0];
      return permuted;
    } else if(n==2){
      permuted[0][0] = array[0];
      permuted[0][1] = array[1];
      permuted[1][0] = array[1];
      permuted[1][1] = array[0];
      return permuted;
    } else {
      int[][] smallerPermuted = new int[nMinus1Factorial][n-1];
      int[]   smallerArray    = new int[n-1];
      int counter = 0;
      for(int ii = 0; ii < n; ++ii){
        for(int jj = ii*nMinus1Factorial; jj < (ii+1)*nMinus1Factorial; ++jj){
          permuted[jj][0] = array[ii];
        }
        counter = 0;
        for(int jj = 0; jj < n; ++jj){
          if(ii==jj) continue;
          smallerArray[counter] = array[jj];
          counter++;
        }
        smallerPermuted = permuteIntegerArray(smallerArray);

        for(int jj = 0; jj < nMinus1Factorial; ++jj){
          for(int kk = 0; kk < n-1; ++kk){             permuted[jj+ii*nMinus1Factorial][kk+1] = smallerPermuted[jj][kk];     } } } }     return permuted;   }   public static int integerFactorial(int base){     int returnValue = base;     while(base > 2){
      base--;
      returnValue *= base;
    }
    return returnValue;
  }
}

This is arguably much cooler than the example solution, though it is a bit long. I feel as though it can be shortened — any suggestions would be appreciated! Sample output below:

15:25 |aww@airy Basic| javac Exercise39.java; java Exercise39

1	 : 7 3 2
2	 : 7 2 3
3	 : 3 7 2
4	 : 3 2 7
5	 : 2 7 3
6	 : 2 3 7
Advertisements