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 11 of 11

Thread: Turning the letters in a character array to integers.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Turning the letters in a character array to integers.

    So the goal of the assignment (or this direct part I'm working on at the moment) is to take an input (in this case, the file just has 'a b c d' in it.) and using a key (say by adding 5) take the ASCII value and encrypt the document. So for example, after turning the file to a string - if the file said "abc" I want the encryption to change the array to first become the ASCII int equivalent, then add the key to that ASCII int (for example: 1) and then back to char to display, in this case, 'bcd'

    In our lab, we did this example and it works the way it should:
    import java.io.*;
    import java.util.*;
    public class Lab8b {
    	public static void main(String[] args) {
    		int charAsNum = 'z'; 
    		charAsNum = charAsNum - 97;
    		int decode = charAsNum + 2;
    		if (decode > 26) {
    			decode = decode % 2;
    		}
    		decode = decode + 97;
    		char numAsChar = (char)decode;
    		System.out.println(numAsChar);
    	}
    }

    And it works perfectly. Using the above logic, this is my current code:
    import java.util.*;
    import java.io.*;
    public class tester{
    	public static void main(String[] args) {
    		char[] test = {'a','b','c','d'};
    		encrypt(test, 5);
    		}
     
    	public static void encrypt(char[] array, int key) {
    		for (int i = 0; i < array.length; i++) {
    			char space = ' ';
    			if (array[i] == space) {
    				// will keep array[i] as a space
    			}
    			else {
    			int charAsNum = array[i];
    			charAsNum = charAsNum - 97;
    			System.out.println(array[i]);
    		}
    		}
    		for (int i = 0; i < array.length; i++) {
    		int decode = array[i] + key;
    		if (decode > 25) {
    			decode = decode % key;
    		}
    		decode = decode + 97;
    		char numAsChar = (char)decode;
    		}
     
     
     
    	}
     
    }

    Nothing happens. What is it that I'm missing?


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

    Default Re: Turning the letters in a character array to integers.

    What do you mean "nothing happens". Is there no output at all appearing? If so put in a bunch of SOP statements throughout your code and see which ones do and don't get displayed. This will help you track down how far your program gets and where it stops working.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Turning the letters in a character array to integers.

    Oops, I guess I should have been clearer, what happens is that the output (displayed thanks to System.out.println(array[i])) is the original array, so it is printing out a b c, what it should be doing is switching to the ascii number of each of them, subtracting 97, and printing that out - so I want to see the output being, in this case, 0, 1, 2

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

    Default Re: Turning the letters in a character array to integers.

    System.out.println(array[i]);
    Since you never change the value stored in the array that is the output you get. Perhaps you should be printing something else or changing the array before printing it.

    BTW I really hate pointless empty if statements.
    if(a == b) {
        do nothing
    } else {
        do something
    }
    is the same as
    if(a != b) {
        do something
    }
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Turning the letters in a character array to integers.

    Ooh yes, that is a useless if statement.

    I had assumed that
    int charAsNum = array[i]
    was supposed to change the array, so that it takes the letter and turn it into a number. Is there any way to make that possible in changing the array (so that it has the desired number) (i.e. array was originally {'a','b','c','d'} and becomes the ASCII value {97,98,99,100}? I thought by identifying each point in the array (with array[i]), that it would reassign the values.

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

    Default Re: Turning the letters in a character array to integers.

    You can simply cast between char and int.
    System.out.println((char) 97);
    System.out.println((int) 'b');
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Turning the letters in a character array to integers.

    Then why doesn't
    			if (array[i] != space) {
    				(int) array[i] = (int) array[i] - 97;
    work? I am trying to cast int array and subtract 97 from the number, however the compiler gives the error
    tester.java:13: error: unexpected type
                                    (int) array[i] = (int) array[i] - 97;
                                    ^
      required: variable
      found:    value
    1 error

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

    Default Re: Turning the letters in a character array to integers.

    You cannot place the cast on the left hand side. You cannot store ints into a char array.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Turning the letters in a character array to integers.

    Thanks, so I got the encrypter working perfectly...now I'm working on making something that decrypts. To do that, I want it to find the most common letter in the array and it will see it's distance from 'e' to find the key (the program is under the assumption 'e' is the most common letter)
    			int[] code = new int[25];
    			for (int i = 0; i < array.length; i++) {
    				int charAsNum = (int)array[i] - 97;
    				charAsNum = charAsNum % 26;
    				for (int a = 0; a < code.length; a++) {
    					if (charAsNum == (97 + i)) {
    						code[i]++;
    					}
    				}
    			}
     
    			for (int i = 1; i < (code.length - 1); i++) {
    				int highest = 0;
    				if (code[i] > code[i-1]) {
    					highest = code[i];
    				}
    				System.out.println(highest);
    			}
    I have this in my method but it's not giving me the highest number (like I want it to) but rather the code[] remains blank from what I can see and it returns me with a bunch of 0s in the command prompt.

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

    Default Re: Turning the letters in a character array to integers.

    If your objective is to count how many a's, b's c's etc in the text then you are making it much harder than it needs to be. Firstly there are 26 letters in the alphabet so your array needs a length of 26 and not 25. Next, simply subtract 'a' from each letter in the text. 'a' - 'a' = 0. 'z' - 'a' = 25. Use the result as the index into your array and increment the count.
    Improving the world one idiot at a time!

  11. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Turning the letters in a character array to integers.

    What we're supposed to do is make it so that it finds the most common letter, that most common later is therefore 'e' (this is the premise). We then (or before, whichever I can get working) convert the letters to their number equivalent (ASCII). Using these numbers and comparing the number of the letter that is most common and the actual value of the most common number (4), we find the key used in the decryption. So for example, if the code finds 'a' is the most common (0), we know that the key will be either + 29 (25 + 4) OR -4 (the difference between a and e). This formula should change all the letters so they adjust in accordance to whatever key we found.

    Is that any clearer?

Similar Threads

  1. how to sort an array of integers(eg using barcode)
    By jovita mateus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2013, 02:09 AM
  2. [SOLVED] One string Text message into array of strings(letters)
    By dianac in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 12th, 2013, 07:23 PM
  3. How to read letters from a word into an array?
    By zhider in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2012, 02:36 AM
  4. Out of bounds error, 2d irregular array of integers
    By Farmer in forum Loops & Control Statements
    Replies: 3
    Last Post: August 1st, 2011, 03:55 PM
  5. Character Array
    By p_dibb in forum Collections and Generics
    Replies: 1
    Last Post: February 16th, 2011, 10:08 AM