File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, this is due in 5 hours.

Please, DoYourOwnHomework

[edited: I found 'y' ]
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2904
1
Java Spring Ubuntu
 
salvin francis
Bartender
Pie
Posts: 1211
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1211
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't name it because the assignment was homework.
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the program running.It works fine, But is there any other way other than switch statement?



 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why is it not working?

What is wrong with this line?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may other problems, but instead of "sentence.charAt(0)" you should have "sentence.charAt(i)".
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:There may other problems, but instead of "sentence.charAt(0)" you should have "sentence.charAt(i)".



other problems like?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, I haven't run the code. Does it work?
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It runs. My problem is every time I input a space it gives me an error.
 
Jay Watson
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why won't it read space?



 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1211
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you finally said it
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1211
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naa
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic