Results 1 to 12 of 12
- 04-20-2012, 06:17 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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();
}
}
}
- 04-20-2012, 12:00 PM #2
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.
- 04-20-2012, 01:18 PM #3
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
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... :)
- 04-20-2012, 04:18 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
- 04-20-2012, 04:30 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 04-27-2012, 03:41 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 04-27-2012, 04:14 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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(); } } }
- 04-27-2012, 07:02 PM #8
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
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?
- 04-27-2012, 08:21 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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. :/
- 04-27-2012, 08:33 PM #10
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
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() loopLast edited by Sierra; 04-27-2012 at 08:40 PM.
- 04-27-2012, 08:40 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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!!!?
- 04-27-2012, 08:43 PM #12
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
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
-
Reading data from file
By Roberto1989 in forum New To JavaReplies: 1Last Post: 04-28-2011, 06:52 PM -
how to append data entered by user to XML file in proper format using JSP
By gurpreet.singh in forum JavaServer Pages (JSP) and JSTLReplies: 8Last Post: 03-09-2011, 12:22 PM -
Help With Outputting Data To A File
By Prodigial Mouse in forum New To JavaReplies: 14Last Post: 01-24-2011, 09:01 PM -
java project help, reading in from a file and adding data to JTable
By Ekul in forum New To JavaReplies: 0Last Post: 11-24-2009, 02:49 PM -
Reading Data from a file
By ramachandran in forum New To JavaReplies: 2Last Post: 10-24-2007, 08:22 AM
Bookmarks