• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader/FileWriter. Help.  RSS feed

 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello world, I need some help!

I've been given a collage task, read a message written in Morse code from a text file and covert it into English, then write it to another text file.

At the moment I am just trying to read the Morse and write it to a file but I've ended up with some of the dreaded red squiggly lines on my last 2 lines. "readFile" and "writeFile".

Does anyone know where I'm going wrong with this?

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've ended up with some of the dreaded red squiggly lines 

Do you get compiler errors that you can copy and paste here to show the errors?
Compiler error messages are usually good at describing the problem.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search for Morse code in this forum; I think two other people seem to have the same assignment. Are you all at the same College?

Why are all three of you using readers and the File class and the exists method? I would use a Scanner and Formatter myself. Why are you using the old method of explicitly closing the readers when you could use try with resources instead? Why am I getting déjà vu about this question? Has somebody else posted exactly the same code Or was it you?
I am sure the exists tests (lines 18 and 61) and the create call (line 61) are unnecessary. If there are any problems, let the exception handling deal with that. If you have problems with exists(), you won't enter the block and you won't know what is wrong.
The Files class doesn't seem to have methods with the names you are using. Have you got your own Files class? I think those calls shouldn't be there. Or anywhere else. I can't see what you need which is outwith that class in the first place. What you need is to read the file, split the text, decide whether it is clear of code, translate it and then write to the new file.
Why have you marked your methods static? Why are you giving that array protected access? I think both those things are mistakes.
 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I've ended up with some of the dreaded red squiggly lines 

Do you get compiler errors that you can copy and paste here to show the errors?
Compiler error messages are usually good at describing the problem.


When you say "compiler errors" would I be right in thinking "debug"?

MorseDecoder [Java Application]
morse.MorseDecoder at localhost:49193
Thread [main] (Suspended (The method readFile(String) is undefined for the type Files))
MorseDecoder.main(String[]) line: 98
C:\Program Files\Java\jre1.8.0_121\bin\javaw.exe (19 Mar 2017, 13:32:46)
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to call the methods in the class you've written, don't put "Files." in front of them, just write:
 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please search for Morse code in this forum; I think two other people seem to have the same assignment. Are you all at the same College?

Why are all three of you using readers and the File class and the exists method? I would use a Scanner and Formatter myself. Why are you using the old method of explicitly closing the readers when you could use try with resources instead? Why am I getting déjà vu about this question? Has somebody else posted exactly the same code Or was it you?
I am sure the exists tests (lines 18 and 61) and the create call (line 61) are unnecessary. If there are any problems, let the exception handling deal with that. If you have problems with exists(), you won't enter the block and you won't know what is wrong.
The Files class doesn't seem to have methods with the names you are using. Have you got your own Files class? I think those calls shouldn't be there. Or anywhere else. I can't see what you need which is outwith that class in the first place. What you need is to read the file, split the text, decide whether it is clear of code, translate it and then write to the new file.
Why have you marked your methods static? Why are you giving that array protected access? I think both those things are mistakes.


This would have been me. I've been trying to do this for a while and posted a previous attempt in this forum before. It wouldn't be a surprise if someone from my college had posted already, a lot of us are struggling.

We're expected to do it with the methods he has taught us. So it's should be something like this... BufferedReader/Writer to read and write the file and 2 sets of the arrays to translate the ".-." to "a".

Although somebody else has attempted it with a scanner and it seemed to be acceptable.
 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:If you want to call the methods in the class you've written, don't put "Files." in front of them, just write:


Thank you! That was so simple. At least it wasn't a curly bracket this time! Haha
 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any pointers on where I should go next with this?
 
Bobby Morrison
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use Arrays to match the characters in the text file?

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Morrison wrote:. . . We're expected to do it with the methods he has taught us. So it's should be something like this... BufferedReader/Writer to read and write the file . . . Although somebody else has attempted it with a scanner and it seemed to be acceptable.
That's all right. Use a buffered reader, though a Scanner is probably quicker to write. You shou‍ld find examples of buffered readers if you go through the appropriate part of the Java™ Tutorials and looks for the section with buffered in its name. Unfortunately that refers back to a previous section with this code in:-I would change that to read:-Note I am using try with resources which ensures the readers are closed correctly. I have also missed out the part which writes the characters to another file. Note how much shorter the entire code is now.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Morrison wrote:. . . 2 sets of the arrays to translate the ".-." to "a". . . .
Now, once I start seeing multiple arrays, I start to think that is bad design. Dreadfully error‑prone. You are going to have to search array1 for a particular String and then note the location you found it. I am not saying any more.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!