• Post Reply Bookmark Topic Watch Topic
  • New Topic

toString returns a null value  RSS feed

 
James Eaker
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm doing a morse-code homework and I always get the null value when I call the toString method.
I'm still on the early chapters of java; and I know my work is opposite of efficient.

So. what I did here is get the first characters which is 0-9 and A-Z from a Morse.txt file.
Since, as of now, I don't know how to read/capture/retrieve a particular word from a sentence in a text file.
What I did is read the first values then put it in a text file and the next values(word) to another text file.
What I'm thinking is compare every line from the first word/string from text file from another text file.

That way, I will compare the String from user input and and compare it to the index of first values copy its index and again, compare its encrypted equivalent which is the series of dots/lines (morse code)
The particular instructions given to us was pretty strict. We have to do the translation at the Translate class and get the input from the Morse (main class). but I don't know how to throw the IO clause in the Translate class,
all of our lectures in the class, the IO clause is done on the main usually. (morse class).

I'm expecting a lot mistakes and efficiency, but I don't want to somewhat start over and just want a helping-hand to pin-point and compensate of what I did so far.. this is driving me crazy
(of course I'm more than happy to rewrite your own efficient code WITH explanation, as I really want to learn this up.top)





Morse.txt file


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Eaker wrote:Ok, I'm doing a morse-code homework...

James,

1. Welcome to JavaRanch.
2. Please DontWriteLongLines (←click). I've broken yours up this time (line 114), but please try to avoid in future. Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Eaker wrote:So. what I did here is get the first characters which is 0-9 and A-Z from a Morse.txt file.

Right, but I don't see any place that you're storing them. You print them out, but you don't put them anywhere for your program to use, so I can't see how it has any way of doing what it's supposed to do.

However, it's only a cursory glance because that's a LOT of code to go through.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never have gotten this far. Never write more than 2-3 lines of code before doing a compile-debug-test cycle. To get to the point where you have 150+ lines of code and only THEN finding out you have a bug like this is just dooming yourself to failure.

I would seriously suggest starting over. Save this and use it as a reference, but re-write everything and test as you code. Make sure things work at each and every step of the way.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you getting null from your toString method? Unless you are passing compare which is null at the time, I can’t see how you can get it to return null. I can’t even see that compare() returns null, so that is a mystery to me.

I trust you were told that the primitive type char represents a letter and nothing else? If so, you have been told wrong. If you look in the Java Language Specification, you find a char is a number (range 0‑65535 inclusive, no negative values). So you can do arithmetic with it. TryThere are certain types of arithmetic which give you sensible results and other types which give nonsense results. Who knows what sort of nonsense you will get from this:-With a little bit of char‑arithmetical jiggery‑pokery, you can turn your chars into array indices, and get a Morse code String out of the array. It is more awkward going backwards. I would use a certain kind of data structure myself, but I am pretty sure you haven’t heard of it yet.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there is an easy way to get from Morse code back to ordinary text, but it only works in Java7. I have only just remembered it.
Note you can divide your Morse code into letters and numbers because all the number codes have 5 characters in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!