Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with Program

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Program

    I wrote this program and while it does everything it should, but I am not sure if it is correct. I need the program to take all the even numbers and write to a .dat file. Then I need to close the file, reopen it, and display the results. After that I need to get the odd numbers and add them to the end of the file. Then reopen the file and display the results. Is this correct?


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

    public class Numbers {
    // The main function
    private static FileOutputStream output;
    private static FileInputStream input;
    private static ArrayList<Integer> inputList;
    static int sumEven = 0;
    static int sumOdd = 0;

    public static void main(String[] args) throws IOException
    {
    createOutputFileEven();
    createInputFile();
    printInputList();
    createOutputFileOdd();
    createInputFile();
    printInputList();
    }
    private static void createOutputFileEven() throws IOException
    {
    //create the output file
    output = new FileOutputStream("numbers.dat");
    //output test values to the file
    for(int i = 2; i <= 100; i +=2)
    {
    sumEven = sumEven + i;
    output.write(i);
    }
    System.out.print("The sum of the Even numbers is " + sumEven + ".\n");
    output.close();
    }//end private static void creatOutputFile
    private static void createOutputFileOdd() throws IOException
    {
    //create the output file
    output = new FileOutputStream("numbers.dat");
    //output test values to the file
    for(int i = 1; i < 100; i += 2)
    {
    sumOdd =sumOdd + i;
    output.write(i);
    }
    //close the output stream
    System.out.print("The sum of the Even numbers is " + sumOdd + ".\n");
    output.close();
    }//end private static void creatOutputFile
    private static void createInputFile() throws IOException
    {
    inputList = new ArrayList<Integer>(10);
    int value;

    input = new FileInputStream("numbers.dat");
    while ((value = input.read()) != -1)
    inputList.add(value);
    }
    private static void printInputList()
    {
    String output;
    if (inputList.size() > 0)
    {
    output = "The values are:\n";
    for(int i = 0; i < inputList.size(); i++)
    if (i == 0)
    output += inputList.get(i);
    else
    output += ", " + inputList.get(i);
    }
    else
    {
    output = "No values in the input list";
    }
    System.out.println(output);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Program

    Is this correct?
    Have you compiled and executed the program and looked at the results?
    Were the results correct?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Program

    Please edit your post, select the code and wrap it in code tags to preserve its formatting.
    See: BB Code List - Java Programming Forums - The Java Community
    Or press the Go Advanced button below the edit box and use the # icon

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM