Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:49+00:00 2026-06-09T16:22:49+00:00

I have started learning Java and am having problem with a simple program that

  • 0

I have started learning Java and am having problem with a simple program that should write some content to numbers.dat file.

I am facing two problems:

  1. The output that I am getting is as follows:

    2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550…

    When it should be:

    For even numbers:

    2,4,6,8,10,12,14,……,98,100

    For odd numbers:

    2,4,6,8,10,12,14,……,98,100,1,3,5,7,9,……..97,99

  2. My second problem is that the output is not getting written to numbers.dat file! It’s all blank.

The question from the book that I am trying to solve is:

Write a program called TextFileIO.java to create a file named numbers.dat. Then create an algorithm that writes all even numbered integers from 1 to 100, separated by a comma.

After the file has been created, close and reopen the file and display the results to the screen. After the results have been displayed append the odd number integers from 1 to 100, separated by a comma to the end of the file. Reopen the file and display the results. The contents of the file should be the even numbers from 1 to 100 separated by a comma followed by the odd number from 1 to 100 separated by a comma. The output of this program would be something like the following:

2,4,6,8,10,12,14,……,98,100

2,4,6,8,10,12,14,……,98,100,1,3,5,7,9,……..97,99

Below is the code that I have tried so far:

textFileIO.java

package package1;

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

//class definition
public class textFileIO{

    //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 I only add a new line at the end, I only need to read the first line.
            System.out.println(reader.readLine());

            //I need to write again, so I close the reader and open the writer.
            reader.close();

            //you need to create another object to append to the file
            //writer.open();
            BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile));

            //This for loop is identical to the previous one, except for odd numbers
            for(int i = 1; i < 100; i += 2) {
                sumOdd += i;
                writer2.write("" + sumOdd + ", ");
            }

            writer.newLine();
            writer.close();

            //The same here. You need to create another reader
            //reader.open();
            BufferedReader reader2 = new BufferedReader(new FileReader(outFile));
            System.out.println(reader2.readLine());

        }
        catch (Exception e) {

        }
    }
}

Edit 2

I changed my code and this is my latest code. I am still having problems.
The console output screen is showing 49 and 50 as output and my numbers.dat file is now showing only comma separated list of odd numbers. What am I doing wrong now?

import java.io.*;
    import java.util.*;
    public class TextFileIO {
        public static void main(String[] args) throws Exception {
            //Clear the contents of numbers.dat if already exists and populated
            File fileOne = new File("numbers.dat");
            fileOne.delete();
            File newFile = new File("numbers.dat");
            newFile.createNewFile();
            //Create writer object
            PrintWriter writer = new PrintWriter(new FileWriter("numbers.dat"));
            //Loop from 1 to 100
            for (int i = 1; i <= 100; i++) {
                //If number is even, write to file  
                if (i % 2 == 0) {
                    writer.print(i + ",");
                }
            }
            //Close writer object.
            writer.close();
            //Read from file
            FileInputStream fileStream = new FileInputStream("numbers.dat");
            DataInputStream in = new DataInputStream(fileStream);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            //Print line
            System.out.println(reader.read());
            reader.close();
            //Write all odd numbers to file
            PrintWriter writer2 = new PrintWriter(new FileWriter("numbers.dat"));
            //Loop from 1 to 100
            for (int i = 1; i <= 100; i++) {
                //If number is odd, write to file  
                if (i % 2 == 1) {
                    writer2.print(i + ",");
                }
            }
            writer2.close();
            //Read from file
            FileInputStream fileStream2 = new FileInputStream("numbers.dat");
            DataInputStream in2 = new DataInputStream(fileStream2);
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2));
            //Print line
            System.out.println(reader2.read());
            reader.close();
        }
     
     
    }

Edit 3: I am still unable to get the desired output. I am still getting the output in console as 49 and 50, and in numbers.dat file, the output is still only odd numbers separated by commas. I am unable to understand what exactly to change in my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:22:51+00:00Added an answer on June 9, 2026 at 4:22 pm

    For the correct number to print you should change

    Even number loop:

    //This for loop handles adding all of the numbers together
    for(int i = 2; i <=100; i += 2) {
    writer.write("" + i + ", ");
    }
    

    Odd number loop:

    //This for loop is identical to the previous one, except for odd numbers
    for(int i = 1; i < 100; i += 2) {
    writer2.write("" + i + ", ");
    }
    

    And replace your following statement:

    System.out.println(reader.read());
    

    with

    System.out.println(reader.readLine());
    

    and you should be done.

    EDIT

    public class textFileIO {
        public static void main(String[] args) throws Exception {
            //Clear the contents of numbers.dat if already exists and populated
            File fileOne = new File("numbers.dat");
            fileOne.delete();
            File newFile = new File("numbers.dat");
            newFile.createNewFile();
            //Create writer object
            PrintWriter writer = new PrintWriter(new FileWriter("numbers.dat"));
            //Loop from 1 to 100
            for (int i = 1; i <= 100; i++) {
                //If number is even, write to file  
                if (i % 2 == 0) {
                    writer.print(i + ",");
                }
            }
            //Close writer object.
            writer.close();
            //Read from file
            FileInputStream fileStream = new FileInputStream("numbers.dat");
            DataInputStream in = new DataInputStream(fileStream);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            //Print line
            System.out.println(reader.readLine());
            reader.close();
            //Write all odd numbers to file
            PrintWriter writer2 = new PrintWriter(new FileWriter("numbers.dat"));
            //Loop from 1 to 100
            for (int i = 1; i <= 100; i++) {
                //If number is odd, write to file  
                if (i % 2 == 1) {
                    writer2.print(i + ",");
                }
            }
            writer2.close();
            //Read from file
            FileInputStream fileStream2 = new FileInputStream("numbers.dat");
            DataInputStream in2 = new DataInputStream(fileStream2);
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2));
            //Print line
            System.out.println(reader2.readLine());
            reader.close();
        }
    }
    

    This worked perfectly for me and here is the output…

    2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,
    1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started learning Java and I have 1 issue: I write a simple
I just started learning Java and I'm having trouble formatting string. In the problem
I am learning java and started with classes and I am now having some
Java script has many falsy values as I started learning. I have a program
I have recently started learning Java EE 6 and I could need some help.
Recently I have started learning rails and was a little surprised that the default
I started learning Java and how to program for Android last night :) So
We have started learning Java in school, and we have been given a few
I've downloaded the JAVA Eclipse IDE and started learning Java. I have a good
G'Day Programmers, I am from Java background however I have just started learning C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.