Results 1 to 15 of 15
  1. #1
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default [HELP] morse code java program

    Hi there . im new here .
    i just need some help .
    i dont know what to do .
    and i saw this site . i know this can help me .

    Write a Java Application that reads a file with Morse code phrases and encodes the phrases into another file with the English Language equivalent . Use one blank between each Morse-coded letter and three blanks between each Morse-coded word .

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: [HELP] morse code java program

    What have you done and where are you stuck?
    Include full error messages, highlighting the line they occur on.
    Any code posted must be in [code] tags [/code] otherwise many people won't bother reading it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class MorseCodes{
    	public static void main(String args[]) throws Exception{		
    		File codeFile = new File("morsecode.txt");
    		File resultFile = new File("result.txt");
    		Scanner codeScan = new Scanner(codeFile);
    		PrintWriter resultWriter = new PrintWriter(resultFile);
    		
    		String givenWord = " ";
    		
    		while(codeScan.hasNext()){
    			givenWord = codeScan.nextLine();
    		}
    		
    		switch(givenWord){
    			case ".-":
    				resultWriter.println('A');
    				break;
    			case "-...":
    				resultWriter.println('B');
    				break;
    			case "-.-.":
    				resultWriter.println('C');
    				break;
    			case "-..":
    				resultWriter.println('D');
    				break;
    			case ".":
    				resultWriter.println('E');
    				break;
    			case "..-.":
    				resultWriter.println('F');
    				break;
    			case "--.":
    				resultWriter.println('G');
    				break;
    			case "....":
    				resultWriter.println('H');
    				break;
    			case "..":
    				resultWriter.println('I');
    				break;
    			case ".---":
    				resultWriter.println('J');
    				break;
    			case "-.-":
    				resultWriter.println('K');
    				break;
    			case ".-..":
    				resultWriter.println('L');
    				break;
    			case "--":
    				resultWriter.println('M');
    				break;
    			case "-.":
    				resultWriter.println('N');
    				break;
    			case "---":
    				resultWriter.println('O');
    				break;
    			case ".--.":
    				resultWriter.println('P');
    				break;
    			case "--.-":
    				resultWriter.println('Q');
    				break;
    			case ".-.":
    				resultWriter.println('R');
    				break;
    			case "...":
    				resultWriter.println('S');
    				break;
    			case "-":
    				resultWriter.println('T');
    				break;
    			case "..-":
    				resultWriter.println('U');
    				break;
    			case "...-":
    				resultWriter.println('V');
    				break;
    			case ".--":
    				resultWriter.println('W');
    				break;
    			case "-..-":
    				resultWriter.println('X');
    				break;
    			case "-.--":
    				resultWriter.println('Y');
    				break;
    			case "--..":
    				resultWriter.println('Z');
    				break;
    			case ".----":
    				resultWriter.println('1');
    				break;
    			case "..---":
    				resultWriter.println('2');
    				break;
    			case "...--":
    				resultWriter.println('3');
    				break;
    			case "....-":
    				resultWriter.println('4');
    				break;
    			case ".....":
    				resultWriter.println('5');
    				break;
    			case "-....":
    				resultWriter.println('6');
    				break;
    			case "--...":
    				resultWriter.println('7');
    				break;
    			case "---..":
    				resultWriter.println('8');
    				break;
    			case "----.":
    				resultWriter.println('9');
    				break;
    			case "-----":
    				resultWriter.println('0');
    				break;
    			default:
    				resultWriter.println(" ");
    				break;
    		}
    		
    		resultWriter.close();
    	}
    }

    on my understanding . in a text file contains morse code will move to another text file in english form .
    so heres my code . i dunno if its correct . but thats my idea . hope someone will correct me or give me an idea .

    when i run my code . does not meet any error . but when i check my file . theres nothing there .

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

    Default Re: [HELP] morse code java program

    When will your while loop exit? At the end of the file when no more text is present to be read.

    Consider putting that switch into a method, and then calling the method *inside* of the while loop after obtaining your String of interest.

  5. #5
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    i did what you said but the same as a result .
    nothing appears in a result file .

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

    Default Re: [HELP] morse code java program

    Quote Originally Posted by g_who View Post
    i did what you said but the same as a result .
    nothing appears in a result file .
    If you've made changes to your code and still need our help, then it would be wise for you to show the new code, and as always show any error messages that you see, if any occur. Otherwise, how are we to guess what you may be doing wrong now?

  7. #7
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    ow .. im sorry . i understand .

  8. #8
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Project{
        public static void main(String args[]) throws Exception{
            Map <String, String> morseCodes = new HashMap <String, String> ();
            
    		File codeFile = new File ("morsecode.txt");
            Scanner codeScan = new Scanner(codeFile); 
    		
    		FileWriter fw = new FileWriter("results.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter outfile = new PrintWriter(bw);
    		
            while(codeScan.hasNext()){
                String letter = codeScan.next();          
                String code = codeScan.next();   
                morseCodes.put(code, letter);
            }
    
            File messageFile = new File("message.txt");
            BufferedReader bufferedReader = new BufferedReader(new FileReader(messageFile));
            String line;
    
            while((line = bufferedReader.readLine()) != null){
                String letter = "";
    
                for(String morseLetter: line.split(" ")){
                    letter = morseCodes.get(morseLetter);
                    outfile.print(letter);
                }
    			if(letter.equals(" ")){
                    // Insert a new line after a three space.
                    outfile.println();
    			}
    			else{
                    // Insert a space between words.
                    outfile.print(" ");
                }
            }
    		outfile.close();
        }
    }

    heres what ive done now .
    my problem is ...
    its always says null exception on the cmd .
    and im planning to print my message on results.txt , but also print null on it .

    when three space occurs on every morse code symbol it should be as another word .
    one space for every letter .

    can anybody help me what should i do

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: [HELP] morse code java program

    What line are you getting the exception?
    What is the full message and stack trace?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    it says Exception in thread "main" java.lang.NullPointerException at project.main(project.java:28)

    and my text file output also null . i think the space .

    but in its on line 19 .

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: [HELP] morse code java program

    What line is line 28?
    Something on that line is null, and you are attempting to access a method or attribute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    Line 28 belongs here

    for(String morseLetter: line.split(" ")){
    letter = morseCodes.get(morseLetter);
    outfile.print(letter);

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: [HELP] morse code java program

    The only thing that can result from that is that line.split() is null.
    And I'm not convinced that can happen.

    You'll need to debug it, sticking System.out.println()'s in there so you can see what's going on (or debug it in an IDE).
    Find out what result line.split() is giving.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    g_who is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: [HELP] morse code java program

    heres what ive done now .
    the only problem is the spacing between word .
    how can i make that on the output file .
    for every word .
    theres a space .

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class Problem1{
    	public static void main(String args[]) throws Exception{
    		try{
    			HashMap <String, String> morseCodes = new HashMap <String, String> ();
    			
    			File codeFile = new File("morsecode.txt");
    			Scanner codeScan = new Scanner(codeFile);
    			
    			while(codeScan.hasNext()){
    				String letter = codeScan.next();
    				String code = codeScan.next();
    				morseCodes.put(code, letter);
    			}
    			
    			Scanner input = new Scanner(System.in);
    			
    			System.out.println("");
    			System.out.println(" * Morse Code To English Translation Program * ");
    			System.out.println("");
    			System.out.println(" - Make sure that every file is (.txt)");
    			System.out.println("");
    			
    			System.out.print("Input File : ");
    			String inputFile = input.next();
    			
    			File translateFile = new File(inputFile);
    			Scanner textFileScan = new Scanner(translateFile);
    			
    			System.out.print("Output File : ");
    			String outputFile = input.next();
    			
    			File resultFile = new File(outputFile);
    			PrintWriter resultWriter = new PrintWriter(resultFile);
    			
    			while(textFileScan.hasNext()){
    				String line = textFileScan.next();
    				
    				if(line != null){
    					String letter = "";
    					
    					for(String morseLetter: line.split(" ")){
    						letter = morseCodes.get(morseLetter);
    						
    						if(letter.equals(" "))
    							resultWriter.println("");
    						if(letter.equals("   "))
    							resultWriter.println(" ");
    					}
    					resultWriter.print(letter);
    				}
    			}
    			resultWriter.close();
    			
    			System.out.println("");
    			System.out.println(" Done.");
    		}
    		
    		catch( FileNotFoundException fnfe ){
    			System.out.println("");
    			System.out.print(" > File Not Found .");
    			System.out.println("");
    		}
    		
    		catch( Exception e ){
    			System.out.println("");
    			System.out.print(" > Error .");
    			System.out.println("");
    		}
    	}
    }

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: [HELP] morse code java program

    Call print() with just a space in it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. (help) morse code java => null
    By lazarat in forum New To Java
    Replies: 18
    Last Post: 09-28-2012, 04:39 AM
  2. Morse Code Help Please!!
    By javaa in forum New To Java
    Replies: 25
    Last Post: 09-28-2012, 03:28 AM
  3. Morse code translation?
    By Koba in forum New To Java
    Replies: 6
    Last Post: 10-09-2011, 11:48 PM
  4. Morse Code Java Program - Help Needed Please!!
    By dungeondragon in forum New To Java
    Replies: 8
    Last Post: 02-25-2011, 04:36 AM
  5. Morse Code
    By Jamison5213 in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 04:56 AM

Posting Permissions

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