Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Help with english to morse code program, I have no idea what I'm doing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with english to morse code program, I have no idea what I Watch "Help with english to morse code program, I have no idea what I New topic
Author

Help with english to morse code program, I have no idea what I'm doing

Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8



Here is the output



I know there are tons of errors, I need your advice. Please, this is due in 5 hours.

Thanks
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Please, this is due in 5 hours.

Please, DoYourOwnHomework

[edited: I found 'y' ]


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3420
    
  12
In your catch block, add tghe line
e.printStackTrace();
This will give you a lot more information about what the problem is.

Also, you are assigning the return value from parseInt to 'x', but your switch statement is checking the value of 'y' which will always be 0.


Joanne
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
The word error is from the catch. I think the reason the word error appears bec. I inputted a character instead of an Integer.
I'm not asking you to do my homework I just need someone to help me.
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
I tried assigning x to the switch statement but it just gives me error. Is there any way to use br.readline w/o the try and catch?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
The word error is from the catch. I think the reason the word error appears ...

If you print out the stack trace like Joanne suggests you'll know for sure. No sense in wasting time if you're under a deadline.

And make sure to read your private messages (to be found under the "My Private Messages" link at the top of the page).


Ping & DNS - my free Android networking tools app
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3420
    
  12
Shen anigans wrote:I tried assigning x to the switch statement but it just gives me error. Is there any way to use br.readline w/o the try and catch?


You don't need x. Assign the value returned from parseInt to 'y'.

One other thing. You can replace your whole switch statement with one line of code. Notice that when 'y' is 1, you print out the value of morse[0], when 'y' is 2, you print out the value of morse[1], etc. Can you see a pattern ?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Convert the string 'val' into the chary:
http://java.sun.com/javase/6/docs/api/java/lang/String.html#toCharArray()

PatienceIsAVirtue and welcome to JR
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917




would be better as:



to be honest, your program does not make sense to me,

instead of doing:



if you want to get code running fast,
you can do something like



'BR' is not a good naming convention, you might want to change that.


The program would have been a lot more simpler if you had used ArrayList

code snippet:




pardon me for any typos in the above code, i wrote it in a giffy.

if you are not allowed to use ArrayList, you can use an array of strings and run a loop to get the index.

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Please avoid abbreviations like "bec".

Salvin Francis, there is a much better data structure than a List. What you are trying to do is to match a letter (eg S) to a pattern (eg "..."). A good place for a beginner to look would be to go through the different interfaces in this Java™ Tutorials section.

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

true, there are more optimized and better approaches.
I was trying to ease out his burden by removing the user entry part to bring him up to speed. I know the data structure you are referring to, but I wonder why you haven't mentioned it here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
I didn't name it because the assignment was homework.
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
I got the program running.It works fine, But is there any other way other than switch statement?



fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Jay Watson wrote:I got the program running.It works fine, But is there any other way other than switch statement?

yes.

First, you may want to 'normalize' your data. if you convert the input to all uppercase, for example, you don't need to account for both 'D' and 'd' as input.

second, note that char variables are really just integer values. Joanne gave you a BIG hint above:
One other thing. You can replace your whole switch statement with one line of code. Notice that when 'y' is 1, you print out the value of morse[0], when 'y' is 2, you print out the value of morse[1], etc. Can you see a pattern ?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8


Why is it not working?

What is wrong with this line?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
There may other problems, but instead of "sentence.charAt(0)" you should have "sentence.charAt(i)".
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
Ulf Dittmer wrote:There may other problems, but instead of "sentence.charAt(0)" you should have "sentence.charAt(i)".



other problems like?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Don't know, I haven't run the code. Does it work?
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
It runs. My problem is every time I input a space it gives me an error.
Jay Watson
Greenhorn

Joined: Jan 27, 2010
Posts: 8
Why won't it read space?



Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 88
It broke because all your code know is MORSE code for only A to Z.
Actually it will break for anything else apart from A-Z like numbers, special characters [@.!.$.%, etc]

If you need to convert only the Alphabets to MORSE code, then checkout



that tells you if the char is a letter or not. If you need to convert the whole input string to Morse code, then you have to think of adding separate arrays for numbers and special characters and handle them


Karthick D V
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Jay Watson wrote:Why won't it read space?


To solve this, ask yourself what letter contains when the program encounters a space, and what the value of letter - 'A' is in that case (hint: the error message already says it). Think about how to handle space, or any other character outside of the alphabet letters.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Since Morse code only uses the 26 letters of the English alphabet, it will only use ASCII characters, so the numbers can be obtained from 'a' % 0x20. Then you can get at the array index but may require -1. Of course if you enter " " as the 0-th element in your array and use a 27-member array, you can put the space back; space is 0x20.
0x20 is what most people call 32.

I was actually thinking about a Map<Character, String> for storing the Morse code, yesterday.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

you finally said it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
salvin francis wrote:you finally said it
Only when there was no risk of it being used as cheating for the assignment. I am sure you had it worked out, didn't you
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

naa
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with english to morse code program, I have no idea what I'm doing
 
Similar Threads
jquery -- code review comparing original and current values on a form
pattren
Doubt on setDeamon().
exercise getServiceByName
Doubt in the code.