Results 1 to 3 of 3
  1. #1
    john89011 is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default help with formatting output while reading a file

    Hi guys,

    I am trying to read a text file and format the output a certain way. This is what I have so far and all it does it outputs the entire text file, I can't figure out how to have it print line by line, do the calculation and format the text, I looked at formatter but it would just print everything on a single line... any input would be greatly appreciated!

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Formatter;
    import java.util.Scanner;

    public class ScannerReadFile {

    public static void main(String[] args) {
    String name, major;

    File file = new File("C:\\Users\\pericjo\\workspace\\john\\src\\da ta.txt");
    try {
    Scanner scn = new Scanner(file);
    while (scn.hasNextLine()) {
    String line = scn.nextLine();
    System.out.println(line);
    }
    } catch (FileNotFoundException e) {
    }
    }
    }


    Here's the text file content:

    John Smith
    CS
    89
    99
    Jonathan Sou
    MATH
    78
    98
    College
    CS
    78
    87
    Math Dept
    MATH
    99
    66
    Comp Science
    CS
    88
    100


    And the output should look like:


    John Smith, CS, 89.0, 99.0
    Jonathan Sou, MATH, 78.0, 98.0
    College, CS, 78.0, 87.0
    Math Dept, MATH, 99.0, 66.0
    Comp Science, CS, 88.0, 100.0
    ----- ---------- ----------
    MATH Math AVG= 88.5 Java AVG= 82.0
    CS Math AVG= 85.0 Java AVG= 95.33333333333333

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: help with formatting output while reading a file

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help with formatting output while reading a file

    Write a format that matches one of those lines.
    Feed it the correct values in a loop (possibly even create an object representing that data).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Formatting Output
    By Rahim2312 in forum New To Java
    Replies: 18
    Last Post: 05-04-2012, 09:45 AM
  2. formatting data output
    By droidus in forum New To Java
    Replies: 16
    Last Post: 04-12-2011, 11:26 PM
  3. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 12:10 PM
  4. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 05:26 PM
  5. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •