Results 1 to 11 of 11
  1. #1
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Talking English to Binary Translator (Please help)

    Hi, so I'm trying to create a program that will prompt for a string by the user, split each character up into an array, translate each character, store it back into a new array, and then display your string translated into binary code. I'm very new to programming, having only started an AP Computer Science class a month ago and done some YouTube learning, I am way over my head. As much as I like the challenge, one solution to an error leads to another error and so on. Can anyone help me out with my code? (By the way, ignore most of those comments, they were just to try and help me out)




    Java Code:
    import java.util.Scanner;
    import java.lang.reflect.Array; //Used for Array.getLength()
    //import java.applet.Applet;
    //import java.awt.*;
    
    public class BinaryTranslator //extends Applet
    {
        public static String[] outputBin;
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            String binUC = "01000001|01000010|01000011|01000100|01000101|01000110|01000111|01001000|" +
                           "01001001|01001010|01001011|01001100|01001101|01001110|01001111|01010000|" +
                           "01010001|01010010|01010011|01010100|01010101|01010110|01010111|01011000|" +
                           "01011001|01011010", binLC = 
                           "01100001|01100010|01100011|01100100|01100101|01100110|01100111|01101000|" +
                           "01101001|01101010|01101011|01101100|01101101|01101110|01101111|01110000|" +
                           "01110001|01110010|01110011|01110100|01110101|01110110|01110111|01111000|" +
                           "01111001|01111010";
            char[] engUC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(), engLC = 
                           "abcdefghijklmnopqrstuvwxyz".toCharArray();
            char[] alphabetArray = new char[26];
            
            //Temporary user interface and prompt for english or binary
            System.out.println("Binary Translator\n");
            String inputString = scan.nextLine();
                           
            //Split the alphabet strings, set values in arrays equal to each other
            String[] binUCArray = binUC.split("\\|"), binLCArray = binLC.split("\\|");
            char[] inputLetters = inputString.toLowerCase().toCharArray();
            
            System.out.println();
            for (int index=0; index<Array.getLength(inputLetters); index++)
            {
                char chLit = inputLetters[index];
                int outputUniVal = Character.getNumericValue(chLit);
                int arrayIndex = outputUniVal-10;
                //int[] letterValues[]; saved incase I need it later
                String outputBin[];
                outputBin[index] = binLCArray[arrayIndex];
                System.out.println(arrayIndex);
            }
            System.out.println("\n" + binUCArray[1] + " = " + engUC[1]);
            System.out.println("Length of string is " + Array.getLength(inputLetters));
            
            //int rows = Array.getLength(outputBin) / 4
            //for (int c=1; c<=4; c++)
            //{
            //    while (
            //}
            System.out.println("\nBinary: ");
            for (int counter=0; counter<Array.getLength(outputBin); counter++)
            {
                System.out.print(outputBin[counter] + " ");
            }
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: English to Binary Translator (Please help)

    Quote Originally Posted by Baconator View Post
    ... one solution to an error leads to another error and so on.
    It sounds like you're creating a bunch of code, then compiling it, and then getting frustrated at seeing a bunch of errors which is the wrong approach to writing code. If you're not using an IDE, then you must create code in small bits, one or two lines at a time, compile after adding these small bits, and then fixing every compilation error you see before adding any new code. Else you may be stuck with an unfixable mess.

    Can anyone help me out with my code?
    Help you how? We need to know what errors are occurring, how it's not working, etc.

  3. #3
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: English to Binary Translator (Please help)

    It seems as if I basically have the program almost done, but I'm getting a "java.lang.NullPointerException" and then it highlights 'outputBin' on line 41

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: English to Binary Translator (Please help)

    Quote Originally Posted by Baconator View Post
    It seems as if I basically have the program almost done, but I'm getting a "java.lang.NullPointerException" and then it highlights 'outputBin' on line 41
    OK, the error message is very helpful and is telling you that outputBin is null. Now check to see where you initialize the variable.

  5. #5
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: English to Binary Translator (Please help)

    Well, that's what I tried doing. Again, since I'm very new to this, I initialized outputBin as a public static Array variable on line 8 and then as an Array variable on line 40. Right?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: English to Binary Translator (Please help)

    Quote Originally Posted by Baconator View Post
    ... I initialized outputBin as a public static Array variable on line 8
    No, you only declare the variable on line 8. To initialize it you would need to set it = to something such as "= new String[someNumber]". So on searching your code, do you do this?

    ... and then as an Array variable on line 40. Right?
    On line 40, you only re-declare the variable which can be a dangerous things in some situations (look up "variable shadowing" to see what I mean).

  7. #7
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: English to Binary Translator (Please help)

    Ohh like instantiation? I appreciate the fact that you're making me learn what I'm doing wrong instead of just posting a solution. I tried inserting
    Java Code:
    int inputLength = Array.getLength(inputLetters);
    String outputBin = new String[inputLength];
    right after line 31, but then I received an incompatible types error...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: English to Binary Translator (Please help)

    Quote Originally Posted by Baconator View Post
    Ohh like instantiation? I appreciate the fact that you're making me learn what I'm doing wrong instead of just posting a solution. I tried inserting
    Java Code:
    int inputLength = Array.getLength(inputLetters);
    String outputBin = new String[inputLength];
    right after line 31, but then I received an incompatible types error...
    Please how the new code, show the new error message, and indicate which line is causing the errors.

  9. #9
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: English to Binary Translator (Please help)

    Java Code:
    import java.util.Scanner;
    import java.lang.reflect.Array; //Used for Array.getLength()
    //import java.applet.Applet;
    //import java.awt.*;
    
    public class BinaryTranslator //extends Applet
    {
        public static String[] outputBin;
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            String binUC = "01000001|01000010|01000011|01000100|01000101|01000110|01000111|01001000|" +
                           "01001001|01001010|01001011|01001100|01001101|01001110|01001111|01010000|" +
                           "01010001|01010010|01010011|01010100|01010101|01010110|01010111|01011000|" +
                           "01011001|01011010", binLC = 
                           "01100001|01100010|01100011|01100100|01100101|01100110|01100111|01101000|" +
                           "01101001|01101010|01101011|01101100|01101101|01101110|01101111|01110000|" +
                           "01110001|01110010|01110011|01110100|01110101|01110110|01110111|01111000|" +
                           "01111001|01111010";
            char[] engUC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(), engLC = 
                           "abcdefghijklmnopqrstuvwxyz".toCharArray();
            char[] alphabetArray = new char[26];
            
            //Temporary user interface and prompt for english or binary
            System.out.println("Binary Translator\n");
            String inputString = scan.nextLine();
                           
            //Split the alphabet strings, set values in arrays equal to each other
            String[] binUCArray = binUC.split("\\|"), binLCArray = binLC.split("\\|");
            char[] inputLetters = inputString.toLowerCase().toCharArray();
            int inputLength = Array.getLength(inputLetters);
            String outputBin = new String[inputLength];
            
            System.out.println();
            for (int index=0; index<Array.getLength(inputLetters); index++)
            {
                char chLit = inputLetters[index];
                int outputUniVal = Character.getNumericValue(chLit);
                int arrayIndex = outputUniVal-10;
                //int[] letterValues[]; saved incase I need it later
                //String outputBin[];
                outputBin[index] = binLCArray[arrayIndex];
                System.out.println(arrayIndex);
            }
            System.out.println("\n" + binUCArray[1] + " = " + engUC[1]);
            System.out.println("Length of string is " + Array.getLength(inputLetters));
            
            //int rows = Array.getLength(outputBin) / 4
            //for (int c=1; c<=4; c++)
            //{
            //    while (
            //}
            System.out.println("\nBinary: ");
            for (int counter=0; counter<Array.getLength(outputBin); counter++)
            {
                System.out.print(outputBin[counter] + " ");
            }
        }
    }
    On line 33, I receive an 'incompatible types' error message, highlighting the whole line and then specifically highlighting "new String[inputLength]" in red.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: English to Binary Translator (Please help)

    Again, you're declaring outputBin twice, don't do that. Only declare it once. If you're going to use it only in main method, then get rid of the declaration in the class by deleting line 8. If you do that you'll need to fix the declaration on line 33 so that the compiler knows that you want it to be an array of String. You'll need to include the [] square brackets after String on the left side (the declaration side of things) of the statement on line 33.

  11. #11
    Baconator is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: English to Binary Translator (Please help)

    Thank you sir. I figured that was probably the problem (not having [] after String on the left) but I wasn't sure, the class is still on chapter 2 and arrays don't come until chapter 6 so I'm ahead by quite a bit. I appreciate your help, the program works now! I'll let you know if I run into more problems as I try to make it look aesthetically nice in the ouput.

Similar Threads

  1. Pig Latin Translator
    By danthegreat in forum New To Java
    Replies: 22
    Last Post: 09-11-2011, 12:42 AM
  2. English to "133t sp34k" translator
    By yalondg in forum New To Java
    Replies: 38
    Last Post: 02-16-2011, 11:28 PM
  3. Custom Font Translator
    By Nerdopolis in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 04:50 AM
  4. Translator hashtable
    By editor35 in forum New To Java
    Replies: 1
    Last Post: 01-11-2009, 03:02 AM
  5. Java multilanguage translator
    By mnprakash in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-05-2009, 09:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •