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

Thread: Printwriter class: displaying File contents to screen with included formatting?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Printwriter class: displaying File contents to screen with included formatting?

    I have a Java assignment that I have been having real trouble with. I almost finished it finally, but the thing is that I have to display weather statistics (based on data calculated or imported from another file) to the terminal and a new file. What I originally did was use all variants of System.out for printing to terminal so I have good formatting. But then I couldn't figure out how to bring that data to the file afterwards, without duplicating each print statement with a new one that just prints the same thing but to the file. So then I re-wrote each print statement just to print to the new file using Printwriter and not the terminal. Then I created a while loop to print everything from the file to the terminal, problem is, it didn't keep any of the formatting. The data is all jumbled up. It looks fine in the file, but it prints everything based on which print statement I used to display all the info at once. So the big Q is, how do I print information from a textfile I've written to, and display it back to the terminal keeping all of its formatting?


  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: Printwriter class: displaying File contents to screen with included formatting?

    How are you controlling the formatting in the file? If you use tabs you can have a problem if the displaying code has different settings for the tabs. Did you remember to restore the lineend characters that are stripped by the readLine method?

    Can you do a screen print of the console with the output and post the file's contents so we can see what you mean by: all jumbled up?

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    I'm mainly using escape characters and printf. I don't know anything about readLine yet. I know why I got what I got, I just don't know how to get what I got in the first picture without printing directly from the script instead of the file.


    This is expected output when printed from script to terminal:
    expected_output.jpg

    Bad output when printed from file to terminal. Using System.out.print(<variable with file contents>)
    bad_output1.jpg

    Bad output when printed from file to terminal. Using System.out.println(<variable with file contents>)
    bad_output2.jpg

  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: Printwriter class: displaying File contents to screen with included formatting?

    How are you reading the data from the file? What class and what method(s)?
    How/where are you adding newline characters to the Strings that are shown on the console?
    I'm mainly using escape characters
    If you use anything but spaces to format your output, different programs will handle things like tab characters differently.

    Can you post the file's contents so we can see how the forum's software displays it?

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    Classes I used:

    import java.io.PrintWriter;
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;

    This is what is being printed.


    outFile.println("\t\t\tHurricanes 1980 - 2006\n");
            outFile.println("Year\tHurricane\tCategory\tPressure (mb)\tWind Speed (mph)");
            outFile.println("========================================================================");
            for (int x = 0 ; x < years.length; x++){
                outFile.printf("%1d %10s %12d %17d %16.1f \n",years[x], names[x], category[x], pressures[x], windSpeed[x]);
            }
            outFile.println("========================================================================");
            outFile.print("Average");
            outFile.printf("%21.0f %17.0f %16.1f\n", catAverage, pressAverage, winSpeeAverage);
     
            outFile.print("Maximum");
            outFile.printf("%21d %17d %16.1f\n", maxCategory, maxPressure, maxWindSpeed);
     
            outFile.print("Minimum");
            outFile.printf("%21d %17d %16.1f\n\n", minCategory, minPressure, minWindSpeed);
     
            outFile.println("Number of Category 1:" + cat1);
            outFile.println("Number of Category 2:" + cat2);
            outFile.println("Number of Category 3:" + cat3);
            outFile.println("Number of Category 4:" + cat4);
            outFile.println("Number of Category 5:" + cat5);
     
            outFile.close();

  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: Printwriter class: displaying File contents to screen with included formatting?

    Can you post the file's contents so we can see how the forum's software displays it?

    Get rid of the tab characters in the headers.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    It looks fine in the file:


    			Hurricanes 1980 - 2006
     
    Year	Hurricane	Category	Pressure (mb)	Wind Speed (mph)
    ========================================================================
    1980      Allen            3               945            115.1 
    1983     Alicia            3               962            115.1 
    1984      Diana            3               949            115.1 
    1985        Bob            1              1002             74.8 
    1985      Danny            1               987             92.1 
    1985      Elena            3               959            115.1 
    1985     Gloria            2               942            103.6 
    1985       Juan            1               971             86.3 
    1985       Kate            2               967             97.8 
    1986     Bonnie            1               990             86.3 
    1986    Charley            1               990             74.8 
    1987      Floyd            1               993             74.8 
    1988   Florence            1               984             80.6 
    1989    Chantal            1               986             80.6 
    1989       Hugo            4               934            138.1 
    1989      Jerry            1               983             86.3 
    1991        Bob            2               962            103.6 
    1992     Andrew            5               922            166.9 
    1993      Emily            3               960            115.1 
    1995       Erin            2               973             97.8 
    1995       Opal            3               942            115.1 
    1996     Bertha            2               974            103.6 
    1996       Fran            3               954            115.1 
    1997      Danny            1               984             80.6 
    1998     Bonnie            2               964            109.3 
    1998       Earl            1               987             80.6 
    1998    Georges            2               964            103.6 
    1999       Bret            3               951            115.1 
    1999      Floyd            2               956            103.6 
    1999      Irene            1               987             80.6 
    2002       Lili            1               963             92.1 
    2003  Claudette            1               979             92.1 
    2003     Isabel            2               957            103.6 
    2004       Alex            1               972             80.6 
    2004    Charley            4               941            149.6 
    2004     Gaston            1               985             74.8 
    2004    Frances            2               960            103.6 
    2004       Ivan            3               946            120.9 
    2004     Jeanne            3               950            120.9 
    2005      Cindy            1               992             74.8 
    2005     Dennis            4               930            149.6 
    2005      Emily            4               929            155.4 
    2005      Irene            2               975             97.8 
    2005    Katrina            5               902            172.7 
    2005      Maria            3               960            115.1 
    2005       Nate            1               979             92.1 
    2005    Ophelia            1               976             92.1 
    2005   Phillipe            1               985             80.6 
    2005       Rita            5               897            172.7 
    2005       Stan            1               979             80.6 
    2005      Vince            1               987             74.8 
    2005      Wilma            5               882            172.7 
    2005       Beta            3               960            115.1 
    2005    Epsilon            1               979             86.3 
    2006    Ernesto            1               995             74.8 
    2006   Florence            1               972             92.1 
    2006     Gordon            3               955            120.9 
    2006     Helene            3               954            126.6 
    2006      Isaac            1               985             86.3 
    ========================================================================
    Average                    2               964            105.1
    Maximum                    5              1002            172.7
    Minimum                    1               882             74.8
     
    Number of Category 1:26
    Number of Category 2:11
    Number of Category 3:14
    Number of Category 4:4
    Number of Category 5:4

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    Quote Originally Posted by mwebb View Post
    But then I couldn't figure out how to bring that data to the file afterwards, without duplicating each print statement with a new one that just prints the same thing but to the file. So then I re-wrote each print statement just to print to the new file using Printwriter and not the terminal. Then I created a while loop to print everything from the file to the terminal
    So you didn't like duplicating printing stuff to screen and to a file (2 steps). Instead you decided to write everything to file, read it back in and then display to screen (3 steps). Plus the added overhead of doing file IO twice making your design less efficient.
    Improving the world one idiot at a time!

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    I would think the design would be more efficient. I tried to minimize the amount of code as much as possible while obtaining the desired result. Otherwise my teacher will tell me to re-correct it when I might as well try and do it right the first time. She usually prefers the script in the most conservative amount of code. Why have 20 more lines of code when I can just add a few more that loops back whats already there? Or some other method? Of course only if that's possible, which leads us back to the big question.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    Writing less code != to more efficient. Performing file IO is expensive in regards to resources. Writing to file once is more efficient than writing to file and then reading it back in. Doesn't it make sense to you? Why bother reading something from a file that you already hold in memory?
    Improving the world one idiot at a time!

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

    mwebb (November 25th, 2011)

  12. #11
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printwriter class: displaying File contents to screen with included formatting?

    I guess I see what your saying. I didn't think of it that way, I just always looked at things in length. Alright, I'll submit my assignment that prints to terminal and file. It does seem ridiculous now, doing it the way I was thinking. The output step of the assignment was pretty vague so I guess the answer to my question was supposed to be easier than I thought. I'll consider this solved. If it was supposed to be done differently I'll come back and show how it should be, for educational purposes to all those Java trainees like myself out there.

Similar Threads

  1. frame not displaying correct contents
    By yemista in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:58 AM
  2. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM
  3. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  4. JDBC code not included into JAR file??
    By friday in forum JDBC & Databases
    Replies: 4
    Last Post: December 17th, 2010, 12:32 PM
  5. Reading in entire contents of text file to one string
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: December 12th, 2010, 07:03 PM