Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Debian Eclipse IDE Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

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

 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scratch that last post. I found it.
 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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...
 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic