• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting variables from Outfiles

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a small problem getting variables from outfiles to produce a Dialog Box. I have to get the number of teams in each outfile and print them out in a dialog box. Along with the best and worst team based on their win percentage. The book I have explains nothing on the subject, nor has the web been any help so far. Anyone have any suggestions on where I'm going wrong? Heres my code:

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.JOptionPane;

public class postlab4
{
public static void main(String[] args)throws FileNotFoundException, IOException
{

double win;
double loss;
double record;
String teamname;
String inputString;
StringTokenizer tokenizer;


BufferedReader inFile = new
BufferedReader(new FileReader("list.dat"));

PrintWriter outFile = new
PrintWriter(new FileWriter("top.dat"));

PrintWriter outLossFile = new
PrintWriter(new FileWriter("bottom.dat"));

DecimalFormat twoDecimal = new
DecimalFormat("0.00");

inputString = inFile.readLine();

tokenizer = new StringTokenizer(inputString);
teamname = tokenizer.nextToken();
win = Integer.parseInt(tokenizer.nextToken());
loss = Integer.parseInt(tokenizer.nextToken());
record = win / (win + loss);

if (record > .5)
outFile.println(teamname + " " + twoDecimal.format(record));
else if (record <= .5)
outLossFile.println(teamname + " " + twoDecimal.format(record));

inputString = inFile.readLine();

while(inputString != null)
{
tokenizer = new StringTokenizer(inputString);
teamname = tokenizer.nextToken();
win = Integer.parseInt(tokenizer.nextToken());
loss = Integer.parseInt(tokenizer.nextToken());
record = win / (win + loss);

inputString = inFile.readLine();

if (record > .5)
outFile.println(teamname + " " + twoDecimal.format(record));
else if (record <= .5)
outLossFile.println(teamname + " " + twoDecimal.format(record));
else;
}
outLossFile.close();
outFile.close();

JOptionPane.showMessageDialog( null, "There are " + " " + " winning teams" + "\n"
+ "There are " + " " + " losing teams" + "\n" + "The best team is: " + " " + " they have a " + " " + " winning percentage" +
"\n" + "The bottom team is: " + " " + " they have a " + " " + " winning percentage", "Football Stats", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can try something like this.
you are trying to parse over list.dat, read each line, parse each line and write some tokens to another file, right ?

BufferedReader inFile = new
BufferedReader(new FileReader("list.dat"));
String inputString;
while ((inputString=inFile.readLine()) !=null)
{
tokenizer = new StringTokenizer(inputString);
teamname = tokenizer.nextToken();
win = Integer.parseInt(tokenizer.nextToken());
loss = Integer.parseInt(tokenizer.nextToken());
record = win / (win + loss);

if (record > .5)
outFile.println(teamname + " " + twoDecimal.format(record));
else if (record <= .5)
outLossFile.println(teamname + " " + twoDecimal.format(record));
}
outLossFile.close();
outFile.close();

I didnt understand why you did inFile.readLine() 2-3 times instead of looping over the inFile.
Also, if outFile and outLossFile are going to contain more than one record, you will have to manually enter a newline after writing each line to the file.

Also, if you use BufferedWriter bw = new FileWriter("filename"), then you the adv. of buffering. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

Was that helpful?
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understand where your coming from I'm new to java so don't really know all the short forms and such so some of the programs i write are really long, for no particular reason. I have the program going like it should taking the list from infile, calculating all the win percentages, then outputting into the two seperate outfiles based on their win percentage. Now what i need to get is the count of the winning and the count of the losing teams within each file, and output it..What I'm thinking is I need a count++; within the if..else statement but I'm not sure.When i put it in there i get a bunch of no if without else errors..no matter how many times ive played with the curly bracers.
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shawn,
Im new to Java too...
Firstly, Id recommend using BufferedWriter instead of PrintWriter.

Every iteration of the loop over the input file will write something to one of the two output files.

If you are interested in getting a line count of each of the files (ie, if each line represents a team), then you need another loop over each of those files in a similar way as I did over the input file and do count++.

So after your first loop of input file...add two more while() loops one each for outfile and outlossfile and within that whileloop do count++

Unless...theres some API to count lines in a file which Im not aware of.

Hoep that was helpful. If not, write back again.
Amit
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On your last post , I'm unclear on what you think i should do with the two while loops to count the teams in each file.

Ok, got the counter to work finally..had a problem with my curly braces..things can get annoying sometimes .
[ October 21, 2004: Message edited by: Shawn Houston ]
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BufferedReader inFile = new
BufferedReader(new FileReader("list.dat"));
String inputString;
while ((inputString=inFile.readLine()) !=null)
{
// do stuff
}

inFile = new BufferedReader(new FileReader(out.dat))
while ((inputString=inFile.readLine())!=null)
{
count++;
}
System.out.println(count);
count = 0 ; //initialize it back to 0

inFile = new BufferedReader(new FileReader(out2.dat))
while ((inputString=inFile.readLine())!=null)
{
count++;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic