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: Writing to output text file

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing to output text file

    Hello all,

    I have been working on a program and it is almost done, however I have 2 small issues:
    Number 1: I need to write my information to an output.txt file, however, I can't seem to get the information to the text file and can't figure out why.
    Number 2: I need to have my output information separated line by line, but right now it is all listed on the same line.

    Here is my program so far
    package tastwob;
     
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.FileReader;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.Collections;
     
    public class TasktwoB{
     
          public static void main(String[] args) throws FileNotFoundException, IOException
        {
         // Prompt for the input and output file names
     
            Scanner console = new Scanner(System.in);
            System.out.print("Please enter the Filename here:   ");
            String inputFile = console.next();
         // Create variables
            ArrayList propids = new ArrayList();
            String line = null ;
            int count = 0;
            double sum = 0;
     
             BufferedWriter pwfo = null;
     
            try{
     
                 File input = new File(inputFile);   
                 BufferedReader in = new BufferedReader(new FileReader(input));
                 pwfo = new BufferedWriter(new FileWriter("C:\\Users\\Bosco\\Documents\\NetBeansProjects\\Tasktwo\\src\\tasktwo\\overview.txt", true));
                 PrintWriter pwo = new PrintWriter(pwfo);
     
                 while ((line = in.readLine())!= null)     
                {
                count++;
                String[] plist = line.split("[\\s}]");
                Double pvalue = Double.parseDouble(plist[2]);
                //for (int i=0; i < plist.length; i++)
    	       {
                    sum+= pvalue;
                   }        
                }
            //int i = 0;
     
     
                System.out.println("Total properties listed:  " + count);
                System.out.println("Total value of properties listed: " +(sum)+ "0" +"\n");
                in.close();
     
            Scanner idfile = new Scanner((input));
              //print property ids
    	       while (idfile.hasNextLine())
    	       {               
    	         line = idfile.nextLine();
    	         String[] fields = line.split("[\\s}]");
    	         String pids = (fields [0]);   
                       {  
                       propids.add(pids);                     
                        //for(int i =0; i<propids.size();i++)   
     
                        in.close();
                       }
                    }
                   Collections.sort(propids);
                   System.out.println(propids);
     
              //close Print Writer
        	       pwo.write("C:\\Users\\Bosco\\Documents\\NetBeansProjects\\Tasktwo\\src\\tasktwo\\overview.txt");
    	       pwo.close();
              }catch(Exception e){
     
                       }
                }
        }

    I am reading information from a text file as follows:

    110001 commercial 500000.00 101
    110223 residential 100000.00 101
    110020 commercial 1000000.00 107
    110333 land 30000.00 105
    110442 farm 200000.00 106
    110421 land 40000.00 107
    112352 residential 250000.00 110

    My output should look like this:

    Total properties listed: 7
    Total value of properties listed: 2120000.00

    110001
    110020
    110223
    110333
    110421
    110442
    112352

    but it looks like this:

    Total properties listed: 7
    Total value of properties listed: 2120000.00

    [110001, 110020, 110223, 110333, 110421, 110442, 112352]

    and it is not writing to the overview.txt file as it should.

    I thank you in advance for any assistance you can provide.


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

    Default Re: Writing to output text file

    it is not writing to the overview.txt file as it should.
    What does it write to the file?

    To have String data be on separate lines in the output file, you need to write a lineend character(\n) at the end of each line. The println() method will do it for you.

    If you want your print out to be one per line you will need a loop to get the items one at a time and print them.
    Last edited by Norm; May 13th, 2012 at 06:47 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The issue hire you are printing out your list... You will need to iterate through each element of the list, the single System.out.println(propids) will not iterate, but will print what you see... Once you loop though the list...

    for (String s : propids) {
    System.out.println(s)
    }

    you should see what you seek...

    Hope this helps.

    Michael.

Similar Threads

  1. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  2. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  3. Help writing to a text file on a website!
    By straw in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 11:02 AM
  4. Issues with writing to text file
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2011, 09:43 AM
  5. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM

Tags for this Thread