Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Morse code program. No output?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of this program is to translate a user inputed sentence into morseCode. It seems like everything is right to me, but I'm simply not getting output! What am I doing wrong, or what should I add/change? Any help is greatly appreciated.

Here is the main class:



TESTER CLASS:




 
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! Could you put your code in [c ode ] tags ? It makes it much easier to read.

As for your code problem, its not clear to me what the return of morse is ? When you use the debugger, are you getting valid return values ?
 
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Since you are comparing the ith character in the input string to the ith character in the alphabet array, it seems like you would only get matches (and output) when the input string resembles the alphabet string. What do you get with an input of ABCXXFG?

.... . .-.. .-.. --- .-- --- .-. .-.. -..
 
Matt Beckner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morse contains the morse code read in from the text file. I understand what your saying with that particular line of code, how do you suppose I go about changing it? Thanks for replying.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest using a data structure like Map<Character, String> morse where the key is a letter, and the value is the morse code for that letter.
 
Ron McLeod
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You code use another loop to scan through the alphabet array looking for a match. If a match is found, you would know the offset where it was found in the alphabet array, and could use that to look-up the corresponding value in the code array.

Update: As Ulf suggested, a Map would be even better if that is an option for you.
 
Matt Beckner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't gotten to maps yet, but could you give an example of the loop you are referring to? It would be a great help.
 
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a suggestion, add a nested for loop with a different loop counter (for example use j)

 
Matt Beckner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I had the same idea earlier and did this. Still no output!

Also my morse code does read in properly from the txt file.
 
Ron McLeod
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your updated translation method looks like it should work, but obviously you will need to be able to get the array of codes loaded-up properly to have your application to perform as expected.
 
Matt Beckner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats my problem. How can I make them load the right way? I can't figure that out.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic