#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Morse code translator(and reading files)


    Okay, So for this program I am supposed to read a "Morse.txt" file into parallel arrays. I just created two files, "Morse.txt" Which reads:

    .----
    ..---
    ...--
    ....-
    .....
    -....
    --...
    ---..
    ----.
    -----
    .-
    -...
    -.-.
    -..
    .
    ..-.
    --.
    ....
    ..
    .---
    -.-
    .-..
    --
    -.
    ---
    .--.
    --.-
    .-.
    ...
    -
    ..-
    ...-
    .--
    -..-
    -.--
    --..

    and an "Alphabet.txt" file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    O
    P
    Q
    R
    S
    T
    U
    V
    W
    X
    Y
    Z

    I have read the files in the arrays. The next this was to create a class, Translate to actually to the translating of user input. I have attempted this, but when I output the translated results in the main nothing is output unless I enter the letter 'Z' in which it therefore outputs the corresponding Morse code. I have output what is in the array's itself and all that should have been read into the file is there.

    Here is my main:

    import java.util.*;
    import java.util.Scanner;
    import java.io.*;

    public class redo
    {
    public static void main(String[]args) throws IOException
    {
    String line2, file2 = "Morse.txt";
    String line, file = "Alphabet.txt";

    File openFile = new File(file);
    File openFile2 = new File(file2);

    Scanner inFile = new Scanner(openFile);
    Scanner inFile2 = new Scanner(openFile2);

    int index = 36;
    char[] charArray = new char[index];
    String[] code = new String[index];

    for(index = 0; index < 36; index++)
    {
    while(inFile.hasNext())
    {
    line = inFile.nextLine();
    charArray = line.toCharArray();
    //System.out.println(charArray[index]);
    }
    }

    for(index = 0; index < 36; index++)
    {
    while(inFile2.hasNext())
    {
    code[index] = inFile2.nextLine();
    //System.out.println(code[index]);
    }
    }

    Scanner keyboard = new Scanner(System.in);

    String userInput;

    System.out.println("Enter something to translate: ");
    userInput= keyboard.nextLine();

    Translate inputTranslate = new Translate(userInput);

    inputTranslate.setInput(userInput);

    inputTranslate.setAlph(charArray);
    inputTranslate.setCode(code);

    System.out.println(inputTranslate.getTranslate());

    inFile.close();
    }
    }






    and here is my class Translate:

    public class Translate
    {
    String input;
    String code[];
    char alph[];

    public Translate(String input)
    {
    this.input = input;
    }

    public void setInput(String input)
    {
    this.input = input;
    }

    public void setAlph(char[] alph)
    {
    this.alph = alph;
    }

    public void setCode(String[] code)
    {
    this.code = code;
    }

    public String getInput()
    {
    return input;
    }

    public String getTranslate()
    {
    String output = "";
    for(int i = 0; i < input.length(); i++)
    {
    for(int index = 0; index < alph.length; index++)
    {
    if(input.charAt(i) == alph[index])
    {
    output = output + code[index];
    }
    }
    }
    return output;
    }


    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    16
    Hi Christinam08,

    You are setting up a for loop. Then inside, you are reading all the way through the file with the while loop, on the first pass of the for loop. On subsequent passes of the for loop, the file is already read, so does not return any more lines. Perhaps try something like this.
    Code:
                index=0;
                while (inFile2.hasNext()) {
                    code[index] = inFile2.nextLine();
                    System.out.println(code[index++]);
                }
    Hope this helps
    slink

IMN logo majestic logo threadwatch logo seochat tools logo