Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InFile to two OutFiles

 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm having a difficult time seeing why I can't get this program to print into two seperate outfiles. I have the program printing into the first outfile fine with the teams that have an above .500 win percentage but the second outfile bottom.dat remains blank. One reason is I think I'm declaring the outfiles wrong, but I'm not real sure. Anyone have any suggestions? 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);

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);
else if (record <= .5)
outLossFile.println(teamname);
else
System.out.println("Program Terminated");



}




outFile.close();
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks more or less OK (at least, insofar as it should do what you want it to do) except that you don't close outLossFile, only outFile. The output may not appear until the files are closed, and if PrintWriter is buffering the data, exiting the program may leave unclosed files still empty.
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you much, very simple mistake on my part. Thanks for pointing that out to me
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another question. I have to output a dialog box saying there is a certain number of winning and losing teams. So I have to declare the teams in the one file winning and the other losing correct? I can't find anything on teh web about declaring something thats within an outfile..Or do i have to use StringTokenizer;?

I also noticed that the first team in the list within the infile isnt printing in either of hte outfiles.. do i have to have an outfile.println above the while loop to get it in the files?

fixed the problem with the first team not printing into the appropriate outfile.

Heres a copy of my new code:



[ October 20, 2004: Message edited by: Shawn Houston ]
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
{
int winning;
int losing;
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);
else if (record <= .5)
outLossFile.println(teamname);

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);
else if (record <= .5)
outLossFile.println(teamname);
}

outLossFile.close();
outFile.close();


}
}
[ October 20, 2004: Message edited by: Shawn Houston ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic