• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please, this is due in 5 hours.


Please, DoYourOwnHomework

[edited: I found 'y' ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't name it because the assignment was homework.
 
Jay Watson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the program running.It works fine, But is there any other way other than switch statement?



 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why is it not working?

What is wrong with this line?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, I haven't run the code. Does it work?
 
Jay Watson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why won't it read space?



 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you finally said it
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
naa
 
reply
    Bookmark Topic Watch Topic
  • New Topic