0

I'm dealing with Caesar Cipher problem and so far all my test cases work, however my problems come when dealing with the list of frequencies. Any help would be appreciated !

    public class CaesarCipher 
    {
    //Percentage frequencies for alphabet
    static double[] table = {8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7,
                7.5, 1.9, 0.1, 6.0, 6.3, 9.1, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1};

    //convert letter to number
    static int let2nat(char c)
    {
        return ((int) c) - 97;
    }

    //convert number to letter
    static char nat2let(int code)
    {
        return (char) (code + 97);
    }

    //shift a letter to another letter
    static char shift(int shftAmt, char c)
    {
        if (let2nat(c) < 0 || let2nat(c) > ((int) ('z' - 'a') + 1) - 1) 
        {
            return c;
        } 
        else 
        {
            // do a safe shift
            int result = (let2nat(c) + shftAmt) % ((int) ('z' - 'a') + 1);
            result += ((int) ('z' - 'a') + 1);
            result %= ((int) ('z' - 'a') + 1);
            return nat2let(result);
         }
    }

    //encodes a string using the given shift amount
    static String encode(int shftAmt, String str)
    {
      char[] encodedStr = new char[str.length()];
      for(int i = 0; i < str.length(); i++)
      {
          encodedStr[i] = shift(shftAmt, str.charAt(i));
      }

      return new String(encodedStr);
    }

    //performs the inverse method to encode
    static String decode(int shftAmt, String str)
    {
      char[] decodedStr = new char[str.length()];
      for(int i = 0; i < str.length(); i++)
      {
          decodedStr[i] = shift(0 - shftAmt, str.charAt(i));
      }

      return new String(decodedStr);
    }

    //Calculates the amount of lowercase letters
    static int lowers(String str)
    {
      int count = 0;
      for(int i = 0; i < str.length(); i++)
      {
          if(let2nat(str.charAt(i)) >= 0 && let2nat(str.charAt(i)) <= 25)
              count++;
      }

      return count;
    }

    //Calculates the number of times a character appears in a string
    static int count(char c, String str)
    {
      int counter = 0;
      for(int i = 0; i < str.length(); i++)
      {
          if(c == str.charAt(i))
              counter++;
      }
      return counter;
    }

    //Calculates the percentage of one integer to another
    static double percent(int num1, int num2)
    {
      return ((float) num1/num2 * 100);
    }

    //Returns the list of percentage frequencies 
    static double[] freqs(String str)
    {
      double[] count = new double[26];
      for(int i = 0; i < str.length(); i++)
          if(let2nat(str.charAt(i)) >= 0 && let2nat(str.charAt(i)) <= 25)
              count[let2nat(str.charAt(i))]++;
      for(int i = 0; i < 26; i++)
          count[i] = percent((int)count[i], lowers(str));
      return count;
    }
}

Above is my code so far, up until figuring out the frequencies..my current output is [D@2a139a55when doing the following System.out.println(freqs("haskellisfun"));

when the output for the following method should be:

that returns the list of percentage frequencies of each of the lower-case letters
’a’ to ’z’ in a string of characters. For example:
freqs("haskellisfun") ->
{8.33333,0.0,0.0,0.0,8.33333,8.33333,0.0,8.33333,
8.33333,0.0,8.33333,16.6667,0.0,8.33333,0.0,0.0,
0.0,0.0,16.6667,0.0,8.33333,0.0,0.0,0.0,0.0,0.0}

Any help would be appreciated, just can't find my error.

Beginner
  • 143
  • 2
  • 4
  • 11
  • 1
    You are getting the result of a default `toString()` method. `array` is not a `List`. – PM 77-1 Nov 01 '15 at 02:52
  • Gotcha, so I'd have to make the array count into a list, specifically a list of doubles? @PM77-1 – Beginner Nov 01 '15 at 02:59
  • Possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Artjom B. Nov 01 '15 at 09:59
  • See also: [Java: Syntax and meaning behind “[B@1ef9157”? Binary/Address?](http://stackoverflow.com/questions/1040868/java-syntax-and-meaning-behind-b1ef9157-binary-address) – Artjom B. Nov 01 '15 at 09:59

1 Answers1

2

What's being printed is the type and hashcode of the array. ([D@2a139a55 means "double array with hashcode 2a139a55")

You can print the array with Arrays.toString() like this:

import java.util.Arrays;
System.out.println(Arrays.toString(freqs("haskellisfun")));
Will
  • 451
  • 6
  • 17