Can you guys run this program, I'm having issues running it.

import java.io.*;
import java.util.*;


//class definition
public class NumberAdder {

	//The main function
	public static void main(String args[]) {

		//This declares a file to open.
		File outFile = new File("numbers.dat");
		
		int sumEven = 0;
		int sumOdd = 0;
		
		//File IO requires a try/catch block to prevent the program from crashing
		try {
		
			//a buffered writer is used to allow us to write to the file.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
			
			//This for loop handles adding all of the numbers together
			for(int i = 1; i < 100; i += 2) {
				sumEven += i + 1;
				writer.writer("" + sumEven + ", ");
			}
			
			//adding a new line and closing the file
			writer.newLine();
			writer.close();
			
			//A buffered Reader is used for reading a new file.
			BufferedReader reader = new BufferedReader(new FileReader(outFile));
			
			//Because we only add a new line at the end, we only need to read the first line.
			System.out.println(reader.readLine());
			
			//we need to write again, so we close the reader and open the writer.
			reader.close();
			writer.open();
			
			//This for loop is identical to the previous one, except for odd numbers
			for(int i = 1; i < 100; i += 2) {
				sumOdd += i;
				writer.writer("" + sumOdd + ", ");
			}
			
			writer.newLine();
			writer.close();
			
			reader.open();
			System.out.println(reader.readLine());
			
		}
		catch (Exception e) {
		
		}
	
	}
}

Your program also doesn't compile. The BufferedWriter doesn't have a method named "writer". In your "for" loop, you call that non-existent method. Probably a typo? Anyway, pay attention to your compiler output - it should have given you an error message about it.

It says the open method is undefined how is that possible? It should be an available option...

import java.io.*;
import java.util.*;


//class definition
public class NumberAdder {

	//The main function
	public static void main(String args[]) {

		//This declares a file to open.
		File outFile = new File("numbers.dat");
		
		int sumEven = 0;
		int sumOdd = 0;
		
		//File IO requires a try/catch block to prevent the program from crashing
		try {
		
			//a buffered writer is used to allow us to write to the file.
			BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
			
			//This for loop handles adding all of the numbers together
			for(int i = 1; i < 100; i += 2) {
				sumEven += i + 1;
				writer.write("" + sumEven + ", ");
			}
			
			//adding a new line and closing the file
			writer.newLine();
			writer.close();
			
			//A buffered Reader is used for reading a new file.
			BufferedReader reader = new BufferedReader(new FileReader(outFile));
			
			//Because we only add a new line at the end, we only need to read the first line.
			System.out.println(reader.readLine());
			
			//we need to write again, so we close the reader and open the writer.
			reader.close();
			writer.open();
			
			//This for loop is identical to the previous one, except for odd numbers
			for(int i = 1; i < 100; i += 2) {
				sumOdd += i;
				writer.write("" + sumOdd + ", ");
			}
			
			writer.newLine();
			writer.close();
			
			reader.open();
			System.out.println(reader.readLine());
			
		}
		catch (Exception e) {
		
		}
	
	}
}

why should it be?
both for writer and for reader, there is NO open() method, so I doubt it was ever supposed to be an available option.

When in doubt, read the manual.
In this case the API doc for the class you are using.

Can I ask you guys what would you use instead? I'm sure you understand I want the program to open it basically.


The program runs fine without the the open method..

what would you use instead

There is no method to use instead.

There is no method to use instead.

What I am trying to do is have the program write the even numbers on the first dat file and then I want it to close and then I want it to write the odd.

Can you explain why you are not able to write to a file, close that file and then write to another file?
If you want to write to a file in append mode, use the constructor that has the append mode flag when you create an object to write to the file.

That's exactly what I'm trying to do. I created the algorithm that adds all the even int from 1 - 100. Now I need the file to create itself and I would close. Once it reads the files I need it to append After the results have been displayed append the odd number integers from 1 - 100.

Is there a question there?

Is there a question there?

The append constructor did not do anything for me at all.. can you show me exactly what needs to be done? I included the writer.append method and its not doing anything doesnt overwrite it at all.

Can you post your code that shows the constructor you are using?
Also explain what you are trying to write to a file. The append mode constructor will cause the output to be written following the current data in the file.
The append() method is something else.

i was using writer.append(); reader.append(); so I did it incorrectly... can you give me some direction?

Look at the I/O classes that have the append mode argument in their constructor.

It is the constructor that specifies that the writing is to be done at the end of the file, not the method.

There is a class that will allow you to position in the file: RandomAccessFile.

You probably don't want to use the RandomAccessFile class. The methods I was referring to could be seek() and setLength()

Can you do me a favor... can you include those methods? So that the program can work, it looks like you know what your doing. I'm running around in circles... seriously..

What methods?
If you want to append to the end of a file, I recommend that you use the class that has the append mode constructor.

I dont know how? are those the words you'd like to hear? I'd be glad if you could help me finish this..

I'm trying to encourage you to read the API doc for the file I/O classes so I won't have to search through them to find the right class.
What classes have you looked at?
Your code has two classes for writing to files. Have you looked at the API doc for those two classes?

When you open the page that your link goes to, on the lower left is a list of ALL the java SE classes. If you find a class in that list and click on it, the doc for that class will be displayed in the main window.
Your posted code has two classes that you are using for writing to files. Find the doc for them in the lower left, click on the name and read the doc.
See if either of them has a constructor that has an append flag in it.

When you open the page that your link goes to, on the lower left is a list of ALL the java SE classes. If you find a class in that list and click on it, the doc for that class will be displayed in the main window.
Your posted code has two classes that you are using for writing to files. Find the doc for them in the lower left, click on the name and read the doc.
See if either of them has a constructor that has an append flag in it.

Okay, I've read and read and no where does it mention the ability to append anything. That's not even an option.

What classes did you read the API doc for their constructors? I'm sure there is an append argument to a constructor.

Do you know what a constructor is?

Well I've always avoided them in C++ and I'm guessing in Java there really important to create instances. I read the bufferedreader/writer ones.

Not to sound rude but do you have experience in this? Just wondering...

I don't get the feeling you are looking in the API doc for the classes you are using.
There are two classes you are using for writing to files:
new BufferedWriter(
new FileWriter(outFile));

You need to read the API doc for these classes.

I have and found
Methods inherited from class java.io.Writer
append, append, append, write, write

I have not said to look at methods. I said look at constructors.
I gave you the names of two classes to look at.
Did you look at the constructors for those two classes?

These are what I found, I highlighted what I would use..
Constructor Summary
BufferedWriter(Writer out)
Creates a buffered character-output stream that uses a default-sized output buffer.
BufferedWriter(Writer out, int sz)
Creates a new buffered character-output stream that uses an output buffer of the given size.

Constructor Summary
FileWriter(File file)
Constructs a FileWriter object given a File object.
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
FileWriter(FileDescriptor fd)
Constructs a FileWriter object associated with a file descriptor.
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.