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 9 of 9

Thread: Cannot get the program to write and display properly

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Cannot get the program to write and display properly

    OK, time to give the noob a hard time. I am writing a program that is supposed to use an algorithm to write all even integers from 1 to 100 to a file, close the file, then display the results. Then id is supposed to append the file with all of the odd integers from 1 to 100, close the file, reopen and display the results. Something like: 1st list - 2, 4, 6, 8, ......., 98, 100 2nd list - 2, 4, 6, 8, ......., 98, 100, 1 , 3, 5, ...., 97, 99

    I get the even(1st) list fine. The 2nd list displays just the even numbers, I cannot get it to display the odd numbers. Sure it is something simple, usually is. My brain is mush right now and I am not seeing it. Thanks for any help!!

    import java.io.*;
     
     
    public class TextFileIO {
    public static void main(String[] args) throws Exception {
     
        //Create newFile
        File newFile = new File("numbers.dat");
        newFile.createNewFile();
     
     
     
        int evenNum = 0;
        int oddNum = 0;
     
       try{
           BufferedWriter writer = new BufferedWriter(new FileWriter(newFile)); 
     
     
        //Loop from 1 to 100
        for (int i = 2; i <= 100; i+=2)
        { 
           evenNum += i + 1;
           writer.write("" + i + ", ");
        }   
     
        writer.newLine();
        writer.close();
     
        BufferedReader reader = new BufferedReader(new FileReader(newFile));
        System.out.println(reader.readLine());
     
        reader.close();
     
        BufferedWriter writer2 = new BufferedWriter(new FileWriter(newFile, true)); 
     
        for(int i = 1; i < 100; i +=2) {  
                oddNum += i;  
                writer2.write("" + i + ", ");  
            }  
        writer2.newLine();
        writer2.close();
     
        BufferedReader reader2 = new BufferedReader(new FileReader(newFile));
     
        System.out.printf(reader2.readLine());
     
       }
     
       catch (Exception e){
     
       }
     }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot get the program to write and display properly

    The default when you write to a file is to erase the old data in the file and replace it with the new data.
    There is an option in the FileWriter class's constructor to change that action to append the new data to the end of the existing file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get the program to write and display properly

    I thought that the code below was supposed to take care of that:

    BufferedWriter writer2 = new BufferedWriter(new FileWriter(newFile, true));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot get the program to write and display properly

    Yes, that looks like what I was talking about.
    Can you post the program's output that shows what you are talking about.
    Add some comments saying what the problem is.


    Have you looked in the numbers.dat file to see what was written?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get the program to write and display properly

    The output is supposed to be:
    2, 4, 6, 8, ........... 96, 98, 100
    2, 4, 6, 8, ........... 96, 98, 100, 1, 3, 5, 7, ..........95, 97, 99

    I get:
    2, 4, 6, 8, ........... 96, 98, 100,
    2, 4, 6, 8, ........... 96, 98, 100,

    It is not writing and/or printing the odd numbers

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot get the program to write and display properly

    Have you looked in the numbers.dat file to see what was written?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Cannot get the program to write and display properly

    Edit: Too Slow

    Have you opened your numbers.dat file to see the contents? Is it there?
    What happens if you read another line after the last print statement?

  8. The Following User Says Thank You to Kewish For This Useful Post:

    jimbo62 (October 3rd, 2013)

  9. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get the program to write and display properly

    OK, the file contains the correct data, even numbers on the 1st line and odd on the 2nd line. And now it wants to work adding a second
    System.out.printf(reader2.readLine());
    Didn't like it before, it just printed the 1st line once. How about this question. How do I get the comma off of the last number on the list? Right now it displays:
    2, 4, 6, ...........98, 100,
    2, 4, 6, ...........98, 100, 1, 3, 5, ............97, 99,

    I know its a cosmetic thing. Thanks for your help Norm and Kewish!!!

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot get the program to write and display properly

    How do I get the comma off of the last number on the list
    Change the logic to put the , before the number except for the first one:
    2
    , 4
    , 6
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    jimbo62 (October 3rd, 2013)

Similar Threads

  1. Write a java program to display the prime numbe 1 to 100?
    By jivanvirginiausa in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 31st, 2013, 01:18 PM
  2. Replies: 0
    Last Post: January 10th, 2013, 01:36 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. trying to write program to display details of employee
    By prathipati in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 13th, 2012, 12:20 AM
  5. [SOLVED] Can't Get Network I/O To Write And Read Properly...
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 36
    Last Post: July 7th, 2011, 10:36 AM

Tags for this Thread