Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Simple code.. why isn't it working?! (no one has been able to solve this for me yet!)

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple code.. why isn't it working?! (no one has been able to solve this for me yet!)

    My code has to read morse code from a text file, ask a user for a sentence, then translate the user's input into morse code. This is what I've written:

     import java.util.*;
       import java.io.*;
     
       public class MorseCode
       {
          public static void main(String[] args) throws Exception
          {
             Scanner inFile = new Scanner(new File("morsecode.txt"));
             Scanner in = new Scanner(System.in);
     
             System.out.print("Please enter a sentence to be translated to morse code: ");
             String userInput = in.nextLine();
     
    	String replacee[] = new String[36];
    	String replacer[] = new String[36];
    	String newInput[] = new String[36];
     
             for(int i = 0 ; i < 36 ; i++)
             {
                replacee[i] = inFile.next();
                replacer[i] = inFile.nextLine().replace(" ", "*");
                newInput[i] = userInput.replace(replacer[i], replacee[i]);
             }
    	System.out.println(newInput);
          }
       }

    In case you're wondering, my text file looks something like this:

    A .- 
    B -... 
    C -.-. 
    D -.. 
    E . 
    F ..-. 
    G --. 
    H .... 
    I .. 
    [continuing with all of the letters]
    0 -----
    1 .----
    2 ..---
    3 ...--
    4 ....-
    5 .....
    6 -....
    7 --...
    8 ---..
    9 ----.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y

    You don't tell us what the program actually does wrong. And since we don't have access to that file, we can't really run it to find out. If you want help with code, you should provide an SSCCE that demonstrates the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y

    I believe you should start by reading the whole text into and array. And it might be easier to use ascii or unicode instead of the actual letters. i.e 65 = A; 66 = b so the text file would be
    //0-9
    48 -----
    ect...
    57 ----.
    //a-z
    65 .-
    66 -...
    ect...

    this way string morseCode[index] would hold the morse codes, i.e. morseCode[65] = ".-", morceCode[66] = "-..."
    then all you need to do is: userCode = (int) userInput; System.out.println(morseCode[userCode]);

    You could even eliminate the numbers in the text file, just remember to list the morse for numbers first and the list the codes for letters last.

    of-corse when looping through the text file you would need to do two loops, one for numbers, using indexCode = i + 47, and the other loop for letters, using indexCode = i + 64. This is made use of by doing the folowing: morseCode[indexCode] = (next string from file)

    This may not be the best way, i'm new to java myself. However this is the way I would have done it in Qbasic. I mean, you cannot use "a" or "b" or "c" as in morseCode[a], morseCode[b] since those would be considered variables and all have the value of 0. And coding it to read the character first and then the code is just a headache, just read the morse code into an array and forget the rest. Also this reduces the disk access. Once you've read it all in you don't need the text file any more. The data could be hardcoded, but I'm assuming reading the text file is part of the assignment.

    note: ascii(48-57) is 0-9 and ascii(65-90) is a-z.
    Last edited by Spidey1980; August 10th, 2011 at 10:00 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y

    I disagree with that advice, but it doesn't really matter as this question is six months old and the OP is most likely long gone. Is there a reason you're resurrecting old threads?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Simple code.. why isn't it working?! (no one has been able to solve this for me y

    sorry was bored did not look at the date

Similar Threads

  1. Code working, but not the way I thought...
    By JLogan3o13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2010, 01:34 PM
  2. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  3. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  4. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM
  5. help me out with the following code to solve the error
    By romilc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2009, 03:45 AM