Results 1 to 11 of 11
- 10-01-2011, 08:50 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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] + " "); } } }
-
Re: English to Binary Translator (Please help)
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.
Help you how? We need to know what errors are occurring, how it's not working, etc.Can anyone help me out with my code?
- 10-01-2011, 09:08 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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
-
Re: English to Binary Translator (Please help)
- 10-01-2011, 09:31 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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?
-
Re: English to Binary Translator (Please help)
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?
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).... and then as an Array variable on line 40. Right?
- 10-01-2011, 09:50 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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
right after line 31, but then I received an incompatible types error...Java Code:int inputLength = Array.getLength(inputLetters); String outputBin = new String[inputLength];
-
Re: English to Binary Translator (Please help)
- 10-01-2011, 10:04 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: English to Binary Translator (Please help)
On line 33, I receive an 'incompatible types' error message, highlighting the whole line and then specifically highlighting "new String[inputLength]" in red.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] + " "); } } }
-
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.
- 10-01-2011, 10:15 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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
-
Pig Latin Translator
By danthegreat in forum New To JavaReplies: 22Last Post: 09-10-2011, 11:42 PM -
English to "133t sp34k" translator
By yalondg in forum New To JavaReplies: 38Last Post: 02-16-2011, 10:28 PM -
Custom Font Translator
By Nerdopolis in forum New To JavaReplies: 3Last Post: 04-18-2009, 03:50 AM -
Translator hashtable
By editor35 in forum New To JavaReplies: 1Last Post: 01-11-2009, 02:02 AM -
Java multilanguage translator
By mnprakash in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-05-2009, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks