Results 1 to 12 of 12
  1. #1
    cknapick is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Unhappy New To Java - HELP with reading data from a file and outputting it into a format

    Please see files below. I can get it to display in the proper format of
    Home: <TeamName> <score>
    Visitor: <TeamName> <score>
    for the first 4 lines of text in the scores.txt file, however when I add a while loop around the code to display that of while(br.readLine !=null), I get a jumbled format. Can anyone help??!? I also need to parse the ints that are read in on the Home and Visitor lines and place an if statement to mark the higher score with an *, i.e. Visitor: Oakland Raiders 30* (if they were the winning team) I know nothing about I/O streams and classes... and having been working on this all day trying to figure it out... HELP!!!?

    I have a file of scores.txt:

    Dallas Cowboys
    21
    San Francisco 49ers
    30
    Denver Broncos
    34
    Oakland Raiders
    0
    New York Giants
    58
    New York Jets
    27
    Philadelphia Eagles
    65
    Pittsburgh Steelers
    7

    and my ReadScores.java:

    import java.io.*;


    public class ReadScores {

    public static void main(String [] args)
    {
    try
    {
    FileReader file=new FileReader("scores.txt");
    BufferedReader br = new BufferedReader(file);



    System.out.print("\nHome: ");
    for(int i=1; i <=2; i++)
    {

    System.out.print(br.readLine()+ " ");

    }

    System.out.print("\nVisitor: ");
    for(int j=1; j <=2; j++)
    {
    System.out.print(br.readLine()+ " ");
    }



    br.close();


    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }

  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: New To Java - HELP with reading data from a file and outputting it into a format

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

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    First: Use code tags please, makes your program code more readable.
    Second: Is this kind of your homework?
    Third: I advise to read on the String.format() method for outputting formatted things and on the Integer.parseInt() Method for your conversion to numbers problem... :)

  4. #4
    cknapick is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    Quote Originally Posted by Sierra View Post
    First: Use code tags please, makes your program code more readable.
    Second: Is this kind of your homework?
    Third: I advise to read on the String.format() method for outputting formatted things and on the Integer.parseInt() Method for your conversion to numbers problem... :)
    No, it is not my homework. It is an exercise in my book that is suggested - to help me understand my hw. ;0)
    Code tags? Do you mean comment lines?

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

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    No:
    Java Code:
    Code tags.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    [code] tags [/code].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    cknapick is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    I see now, thanks!

    Java Code:
    
    	
    	I have a file of scores.txt:
    
    Dallas Cowboys
    21
    San Francisco 49ers
    30
    Denver Broncos
    34
    Oakland Raiders
    0
    New York Giants
    58
    New York Jets
    27
    Philadelphia Eagles
    65
    Pittsburgh Steelers
    7
    
    and my ReadScores.java:
    
    import java.io.*;
    
    
    public class ReadScores {
    
    public static void main(String [] args)
    {
    try
    {
    FileReader file=new FileReader("scores.txt");
    BufferedReader br = new BufferedReader(file);
    
    
    
    System.out.print("\nHome: ");
    for(int i=1; i <=2; i++)
    {
    
    System.out.print(br.readLine()+ " ");
    
    }
    
    System.out.print("\nVisitor: ");
    for(int j=1; j <=2; j++)
    {
    System.out.print(br.readLine()+ " ");
    }
    
    
    
    br.close();
    
    
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }

  8. #8
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    Well done. Ok, so what is the problem now? What is the output you get and what is the output you would expect?

  9. #9
    cknapick is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    Hi Sierra! What I need it to do is print out in the format:

    Home: Dallas Cowboys 21
    Visitor: San Francisco 49ers 30

    For every pair of teams, going straight down the line. Would you mind taking a look at my most recent post too? I fear that the things I mess up in java are not that hard... I think I over-think things. :/

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    Well you seem not to mess up anything - the only thing is that you print the first two only, is that your problem?

    EDIT: If so include both for loops into a while() loop
    Last edited by Sierra; 04-27-2012 at 07:40 PM.

  11. #11
    cknapick is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    No, the way it outputs is the problem. The way it prints, as it is currently coded, is that it skips the entire first line Home: Dallas Cowboys 21 and goes into the the next team's name and score... its all jumbled... weird. Here was my original statement:

    Please see files below. I can get it to display in the proper format of
    Home: <TeamName> <score>
    Visitor: <TeamName> <score>
    for the first 4 lines of text in the scores.txt file, however when I add a while loop around the code to display that of while(br.readLine !=null), I get a jumbled format. Can anyone help??!? I also need to parse the ints that are read in on the Home and Visitor lines and place an if statement to mark the higher score with an *, i.e. I know how to parse Integers I am just having a hard time figuring out an equation that will evaluate each pair of teams on a case by case basis. ex. Visitor: Oakland Raiders 30* (if they were the winning team) I know nothing about I/O streams and classes... and having been working on this all day trying to figure it out... HELP!!!?

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: New To Java - HELP with reading data from a file and outputting it into a format

    To help you we first need to understand your problem... it outputs everything right. Your only fault is in the code you did NOT post, which is the WHILE() loop. Guy, if you have a problem post the code that causes the problem not the code working well. I mixed it up.

    if you use while(br.readLine () != null) of course it skips the line because it reads it here and you throw it away because you do not use it. How about using while(br.ready()) for your purpose?

Similar Threads

  1. Reading data from file
    By Roberto1989 in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 05:52 PM
  2. how to append data entered by user to XML file in proper format using JSP
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 03-09-2011, 11:22 AM
  3. Help With Outputting Data To A File
    By Prodigial Mouse in forum New To Java
    Replies: 14
    Last Post: 01-24-2011, 08:01 PM
  4. Replies: 0
    Last Post: 11-24-2009, 01:49 PM
  5. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 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
  •