Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
Like Tree1Likes

Thread: Morse Code Help Please!!

  1. #21
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

  2. #22
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    Thanks
    Last edited by javaa; 08-19-2012 at 12:53 AM.

  3. #23
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    Thank you guys so much for helping me!!! I got the program to work!!! Thanks <333
    Java Code:
    public class Project1CTY {
    	public static void main( String [] args ) {
    		int answer = Input.getInt( "If you wish to translate Morse Code into English, enter the number 0, \n if you wish to translate English into Morse Code, enter the number 1: ");
    
    		if (answer == 0) {
    			String x = Input.getString("You chose to translate Morse Code into English. Input the Code below. \n When inputting Morse Code, separate each letter/digit with a single space, and delimit multiple words with a “|”. \n For example, - --- | -... . would be the Morse Code input for the sentence “to be”.");
    			EnglishConvert(x);
    		}
    		else if(answer == 1) {
    			String y = Input.getString("You chose to translate English into Morse Code. Enter the sentence you wish to convert below. \n When inputting English, separate each word with a blank space.");
    			MorseConvert(y);			
    		}
    		else
    			System.out.println( "Invalid Entry" );
    	}
    
    	public static void EnglishConvert(String x) {
    		String[] morseArray1 = x.split(" | ");
    		for (int i = 0; i < morseArray1.length; i++) {
    			int w = morsedatabase(morseArray1[i]);
    			System.out.print( convert1(w) );
    		}
    	}
    
    	public static int morsedatabase( String a ) {
    		String[] morseData = {"|", ".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
    		int g = 0;
    		for (int p = 0; p < morseData.length; p++) {
    			if (morseData[p].equals(a)) {
    				g = p;
    			}
    		}
    		return g;
    	}
    
    	public static String convert1(int m) {
    		String[] letters = { " ", "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    		return letters[m];
    	}
    
    
    	public static void MorseConvert(String y) {
    		String[] englishArray1 = y.split("");
    		for (int i = 0; i < englishArray1.length; i++) {
    			int w = convert2(englishArray1[i]);
    			System.out.print( morsedatabase2(w) + " ");
    		}
    	}
    
    	public static String morsedatabase2( int m ) {
    		String[] morseData = {"|", ".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
    		return morseData[m];
    	}
    
    	public static int convert2( String a ) {
    		String[] letters = { " ", "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    		int g = 0;
    		for (int p = 0; p < letters.length; p++) {
    			if (letters[p].equals(a)) {
    				g = p;
    			}
    		}
    		return g;
    	} 
    }

  4. #24
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Morse Code Help Please!!

    Great that you got it to work.

    Why have you declared the arrays inside the methods. This means that everytime the method is called a new array is created. A better solution would be to declare them as instance variables and make your methods non-static, the OO way.

  5. #25
    lazarat is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: Morse Code Help Please!!

    can somebody take a look at my code too and tell me what m i doing wrong because i get "null' when i run the program...
    import java.util.*;
    import java.io.*;
    public class lab3 {

    /**
    * @param args
    */
    public static void main(String[] args)throws IOException {
    // TODO Auto-generated method stub

    Scanner scan=new Scanner (System.in);

    //putting user input in a character array
    char [] letters = new char [26];
    String [] code = new String [26];

    //object creation
    Scanner infile = new Scanner (new File ("morsecode.txt"));
    FileWriter fw = new FileWriter("results.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter outfile = new PrintWriter(bw);

    int ctr = 0;
    while(infile.hasNext()){
    letters [ctr] = infile.next().charAt(0);
    code [0] = infile.next();
    ctr++;
    }
    System.out.println("enter word or enter 'done' to quit");
    String word= scan.nextLine();
    while(!word.equals("done")){
    word = word.toUpperCase();
    outfile.print(word + " ");
    for(int i=0; i<word.length(); i++){ // use a loop to iterate over user input stored in word
    char let=word.charAt(i);
    boolean found=false;
    for (int j=0; j<letters.length && !found; j++){
    if (letters[j]==let){
    System.out.println(code[j]+" ");
    outfile.print(code[j] + " ");
    found=true;
    }
    }
    }
    outfile.println();
    System.out.println("\nEnter another word or enter 'done' to quit");
    word=scan.next();
    }
    outfile.close();
    }
    }

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

    Default Re: Morse Code Help Please!!

    Quote Originally Posted by lazarat View Post
    can somebody take a look at my code too and tell me what m i doing wrong because i get "null' when i run the program...
    Please do not hijack someone else's thread. Instead create your own thread by simply clicking on the new question button in the forum. It doesn't cost anything and doesn't confuse the volunteers here like your hijack will.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Morse code translation?
    By Koba in forum New To Java
    Replies: 6
    Last Post: 10-09-2011, 11:48 PM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 04:36 AM
  4. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 04:56 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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