Home > Default > Assigning letters of the alphabet to characters from a .txt file

Assigning letters of the alphabet to characters from a .txt file

October 11Hits:0
Advertisement
To say in advance, I am relatively new to java, and really have trouble wrapping my mind around some of these things sometimes, so bear with me if I sound like Im rambling or don't know what I'm talking about. What Im trying to do is basically get some input from the user, read from a file of letters (one letter per line) in the order that they should be assigned to the alphabet. So the list starts off like this (parenthesis are just for example and are not in the file):
y (a)
7 (b)
z (c)
x (d)
c (e)
o (f)
5 (g)
4 (h)
j (i)
r (j)
If the user types in "abe", I need to find a way for the system to translate that to "y7c". Im also looking to do this the opposite way, so the user types in "y7c" and the output is "abe". I've got it separated so that you type in "translate" to convert normal text to the code, and "convert" to start the process change the code to normal text.
Its the actual translating and converting where Im stuck. Heres my all my code (not all of it is mine, but for the most part it is):
public class cypher1 {
     static Scanner query = new Scanner(System.in);
     String alpha[]; {
               alpha = new String[26];
               alpha[0]= "a";
               alpha[1]= "b";
               alpha[2]= "c";
               alpha[3]= "d";
               alpha[4]= "e";
               alpha[5]= "f";
               alpha[6]= "g";
               alpha[7]= "h";
               alpha[8]= "i";
               alpha[9]= "j";
               alpha[10]= "k";
               alpha[11]= "l";
               alpha[12]= "m";
               alpha[13]= "n";
               alpha[14]= "o";
               alpha[15]= "p";
               alpha[16]= "q";
               alpha[17]= "r";
               alpha[18]= "s";
               alpha[19]= "t";
               alpha[20]= "u";
               alpha[21]= "v";
               alpha[22]= "w";
               alpha[23]= "x";
               alpha[24]= "y";
               alpha[25]= "z";
     int num[]; {
          num= new int[9];
          num[0]= 1;
          num[1]= 2;
          num[2]= 3;
          num[3]= 4;
          num[4]= 5;
          num[5]= 6;
          num[6]= 7;
          num[7]= 8;
          num[8]= 9;
          num[9]= 0;     
     public static void main(String[] args) {
     try {
          FileInputStream fstream = new FileInputStream("C:\\Documents and Settings\\HP_Administrator\\workspace\\Cypher\\Cypher.txt");
          DataInputStream in = new DataInputStream(fstream);
               BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br.readLine()) != null) {
          System.out.println("Enter 'translate' or 'convert'");
          in.close();
     } catch (Exception e) {
          System.err.println("Error: " +e.getMessage());
     String input = query.nextLine();
     if (input.equals("translate")) {
          System.out.println("You chose to Translate");
          String transTxt;
          System.out.println("Enter text to translate: ");
          transTxt = query.nextLine();
          String[] newStringArray = new String[transTxt.length()];
          for(int i = 0;i < transTxt.length();i++){
             newStringArray[i] = transTxt.substring(i, i+1);
          if (input.equals("convert")) {
               System.out.println("You chose to convert");
               String conTxt;
               System.out.println("Enter text to convert: ");
               conTxt = query.nextLine();
               String[] newStringArray = new String[conTxt.length()];
               for(int i = 0;i < conTxt.length();i++){
                  newStringArray[i] = conTxt.substring(i, i+1); 
}I've got the code from the "cypher" file read, but Im thinking I might run across some issues since the code that reads from the file is not mine, and may not be exactly what I need. But my main question is how do I match each character from the input with its other character from the file? It's there that I am completely lost. I don't even have a clue how to start that.
Any advice at all would be amazing!

Answers

Cypher220 wrote:
Any advice at all would be amazing!First, I agree with Kevin that Maps (especially HashMaps) would probably be ideal but, assuming you're not familiar with them just yet and want to use arrays, some pointers:
1. Since your translator is character-based, use arrays of characters (ie, char or Character), not Strings. For example, your 'alpha' array could be set up with:
private char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray();Characters can be extracted from Strings using [String.charAt()|http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/String.html#charAt%28int%29].
2. Finding characters will be much quicker if the array is sorted. Have a look at the Arrays class for some useful methods.
3. Decide what you want to do if the supplied string contains an invalid character. Common practice is to throw an Exception, but you might want to display a message and get the user to re-enter.
4. You use the terms "translate" and "convert". I'd suggest "encode" and "decode" would be more explicit to a user.
5. I'm not quite sure what your 'num' array is meant to do. If numbers are included in your code, then they should be included in your 'alpha' array (which makes the name a bit misleading).
6. You don't appear to be encoding spaces. Is this what you want?
7. You need a way of associating a character in one array with its corresponding character in the other. One way to do that would be to create a nested class:
private static class AssociatedCharacter {
   private char c;
   private int associatedIndex;
}and then set up arrays of 'AssociatedCharacters'. An alternative is to set up an array of indexes for each array of characters, viz:
private char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray();
private char[] encoded = new char[alpha.length]; // load with your "encoded" characters
private int[] alpha2encoded = new int[alpha.length];and then load it with the index that corresponds to it's encoded version in the other array. Obviously, you'll need an 'encoded2alpha' to go the other way, and it may be a bit more difficult since 'encoded' should be sorted too.
HIH
Winston

Read other 3 answers

Tags:

Related Articles

  • Assigning letters of the alphabet to characters from a .txt fileOctober 11

    To say in advance, I am relatively new to java, and really have trouble wrapping my mind around some of these things sometimes, so bear with me if I sound like Im rambling or don't know what I'm talking about. What Im trying to do is basically get so

  • Special Characters and Importing .TXT filesNovember 30

    I am trying to import a .txt file that has special Characters.  The ñ works correct, but the ® does not import correctly.  Any Ideas?What is strange is that if I open the .txt file up on a windows machine the ® is there.  When placing your text file,

  • How to Type Diacritical Alphabets / Special CharactersNovember 30

    Dear, I am looking for the solution of following issues in MS Word and Excel 2007 Onward Applications: Our group members currently working in typing English articles on different chapters / topics and we are currently facing some problem with the sam

  • Reading characters from a text file into a multidimensional array?October 11

    I have an array, maze[][] that is to be filled with characters from a text file. I've got most of the program worked out (i think) but can't test it because I am reading my file incorrectly. However, I'm running into major headaches with this part of

  • Limit on the number of characters of the Input file of Input AgentOctober 11

    HI I am working on the Input Agent of one of my IPM applicaton. I just want to know what would be the limit on the number of characters in the input file of input agent. That means that what should be the total count limit of the characters of the In

  • Problem to visualize characters in a pdf fileOctober 11

    Hello , i'm from italy i have a problem with Adobe Reader X. I don't visualize some characters in a pdf file. On the arrow in the page there are numebrs and letter , actually there is a grey line .In the first page there is this phrase: If grey lines

  • Chiness bold characters in the PDF file is not apperaingOctober 11

    Hi, facing the problem, when i went to PO Display (ME23N) - Message -> selecting the output message like NEUA , -> Edit -> Display Originals . This time the chiness characters are displaying in the PDF file, but not in the bold format. Define the

  • Non printable characters in a text file..October 11

    hi, How to get blank lines and non-printable characters and remove those characters from the text file being uploaded from application server . thanks, Anil.Take a look at the constants in cl_abap_char_utilities. A simpler solution would be to ask fo

  • Removing the Control Characters from a text fileOctober 11

    Hi, I am using the java.util.regex.* package to removing the control characters from a text file. I got below programming from the java.sun site. I am able to successfully compile the file and the when I try to run the file I got the error as -------

  • Removing non printable characters from an excel file using powershellOctober 11

    Hello, anyone know how to remove non printable characters from an excel file using powershell? thanks, jose.To add - Excel is a binary file.  It cannot be managed via external methods easily.  You can write a macro that can do this.  Post in the Exce

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.940 s.