wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Write a program that reads and writes from binary or text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write a program that reads and writes from binary or text files" Watch "Write a program that reads and writes from binary or text files" New topic
Author

Write a program that reads and writes from binary or text files

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So my pseudo code goes...

Enter name for a file
Choose binary or text
Choose read or write
If read, open file
If write, ask for input to write a line of code
When done with write, ask user if they want to write another line
Ask user if they want to do another file


I know I am having troubles with my if else statements. My errors are...

--------------------Configuration: <Default>--------------------
F:\Object Java\Binary.java:68: error: 'else' without 'if'
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:68: error: not a statement
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:68: error: ';' expected
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:92: error: not a statement
}else(bt.equals("t") || bt.equals("T")){
^
F:\Object Java\Binary.java:92: error: ';' expected
}else(bt.equals("t") || bt.equals("T")){
^
F:\Object Java\Binary.java:124: error: 'else' without 'if'
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:124: error: not a statement
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:124: error: ';' expected
else(rw.equals("w") || rw.equals("W")){


Can anyone help? Thanks in advance.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
The else without if error usually means you have too many ; after the if. Maybe like this if (...);
Correct that error, which appears first, and see how many error messages vanish thereafter.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I checked that and also moved my do while loops. I'm getting this error now...



--------------------Configuration: <Default>--------------------
F:\Object Java\Binary.java:65: error: not a statement
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:65: error: ';' expected
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:90: error: not a statement
}else(bt.equals("t") || bt.equals("T")){
^
F:\Object Java\Binary.java:90: error: ';' expected
}else(bt.equals("t") || bt.equals("T")){
^
F:\Object Java\Binary.java:122: error: not a statement
else(rw.equals("w") || rw.equals("W")){
^
F:\Object Java\Binary.java:122: error: ';' expected
else(rw.equals("w") || rw.equals("W")){
^
6 errors

Process completed.






Ben Ooms
Ranch Hand

Joined: May 16, 2011
Posts: 40

Also you can use a beter equal method for testing the letter entered.

Instead of:


write:


What you can do to have less errors when writing if's is to start with a empty if like:


and then fill the conditions and statements. With this method youre less prone for brackets error. You can use the same for classes, methods and so on.
regards,
Ben
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Luke Stamper wrote:

You can't add a condition after else. You can add another nested if by simply turning those elses into else ifs.

In short, the syntax is like this:
where you can of course leave out lines 2, 3 and 4.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Ok...here's where I'm at. I'm getting this error in both output classes...


--------------------Configuration: Exception - JDK version 1.7.0 #5 <Default> - <Default>--------------------
F:\Object Java\OutputBinary.java:31: error: incompatible types
throw new MyException("The file could not be created.");
^
required: Throwable
found: MyException
1 error

Process completed.


I'm not sure why I am getting that.


Secondly,

I am getting these errors from my Binary class.


--------------------Configuration: Exception - JDK version 1.7.0 #5 <Default> - <Default>--------------------
F:\Object Java\Binary.java:23: error: cannot find symbol
OutputText mto = null;
^
symbol: class OutputText
location: class Binary
F:\Object Java\Binary.java:24: error: cannot find symbol
OutputBinary mbo = null;
^
symbol: class OutputBinary
location: class Binary
F:\Object Java\Binary.java:36: error: cannot find symbol
mbo = new OutputBinary(fname);
^
symbol: class OutputBinary
location: class Binary
F:\Object Java\Binary.java:40: error: incompatible types
catch (MyException e){
^
required: Throwable
found: MyException
F:\Object Java\Binary.java:91: error: cannot find symbol
mto = new OutputText(fname);
^
symbol: class OutputText
location: class Binary
F:\Object Java\Binary.java:95: error: incompatible types
catch (MyException e){
^
required: Throwable
found: MyException
6 errors

Process completed.


I have been working on this problem all day, modifiying and changing code. I feel like I am close to having a program that successfully asks the user to create and read binary/text files, but I could really use some help in dealing with these errors. I really have no idea what I am doing wrong.

Thanks in advance,

Luke


Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So the code compiles. The program almost runs. I am getting a few errors. I know something is not right. I am in the process of debugging it. Does anyone see any pressing errors?

Here's what happens when I run it...

--------------------Configuration: <Default>--------------------
Enter the file name.
fileOne
Chose binary or text file (b/t):
k
Chose read or write (r/w):
w
Enter a line of information to write to the file:
Hello World
Exception in thread "main" java.lang.NullPointerException
at Binary.main(Binary.java:68)

Process completed.



Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
Look over your if...else's again. I went over them and found one last extra else statement. After that, it ran just fine for me.


Jason K. (AC)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
Why have you got such a long main method? You ought to divide that code up into several smaller methods.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Why are all you classes extending Exception when they are not exceptions?

I think that you have to look into what Extends actually does.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Jason,

Can you elaborate on that? I cannot find my bracket error and it is driving me insane.

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Scratch that last post. I found it.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Right now, my issues are...
1. Read text and read binary are not working
2. If a user selects no after writing to a text file, the program should start over

I have commented out where I think the problems are. Can anyone help me?


Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5




Maybe I just missed it, but where do you actually read and do something with the contents of the file? And what does "not working" mean? Try starting with reading the entire file line by line and printing to console.

Also...

This will call the toString() method on the ObjectInputStream. Unless I miss my guess, the you're going to get ObjectInputStream@someNumber as a result. Try printing the contents of the stream rather than the stream itself?

If it's still not "working", post back with a clear description of what "not working" means - are you getting an error, is the output not what you expect and if so what DO you expect to see...


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So everything appears to be working except for when I try and show the contents of a text file, I get this error:

Exception in thread "main" java.lang.NullPointerException
at Binary.main(Binary.java:118)

and when I try and show the contents of a binary file, this exception is printed...

catch (IOException e){
System.out.println("The file is blank, please write in it.");
}

I can't seem to grasp how to display the contents of a .txt or a .dat file.


Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

For the null pointer exception, take a look at line 118 of your source. Ask yourself what are all the objects in that line, and how could one of them be null.

For the binary file, examine the cause of the exception, usually given in e.getMessage(). That should give you a start on what you need to do. If that's not helpful, try printing a message after each line where the problem is occurring to see which line is throwing the exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a program that reads and writes from binary or text files
 
Similar Threads
Reading File Problem
How can I change the character in mid of the file
Updating File
reading input from a file into a array