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

Get a character from its Unicode index

I’ve been working my way through these Java exercises at w3resource.com, and at one point I wanted to be able to get a particular Unicode character from its index. For example, I wanted to turn the integer 65 (or 41 in hexadecimal) into the unicode character U+0041, or a capital letter “A”. This was for a program which would convert a given decimal number to its hexadecimal equivalent:

import java.util.Scanner;

public class decimalToHex {
  public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    System.out.print("Insert a positive integer: ");
    int num = in.nextInt();
    String hex = "";
    int uni = 0;

    while(num != 0){
      uni = num % 16;
      if(uni > 9){ hex = Character.toString((char)(uni+55)) + hex; }
      else       { hex = Character.toString((char)(uni+48)) + hex; }
      num /= 16;
    }

    System.out.println(hex);

    return;
  }
}

As you can see in the code above, the way I accomplished this was to typecast the integer values (uni+55 or uni+48) to a char, then convert that char to a String. Another way to do this (as was done in the example solution) is to define a 16-character or a 16-String array of 0-9 and A-F, and just add the appropriate character to “hex” with:

String[] myarray = { "0", "1", "2" ... "D", "E", "F" }
...
hex = myarray[uni] + hex;