aspose file tools*
The moose likes Beginning Java and the fly likes InFile to two OutFiles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "InFile to two OutFiles" Watch "InFile to two OutFiles" New topic
Author

InFile to two OutFiles

Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
Thank you much, very simple mistake on my part. Thanks for pointing that out to me
Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InFile to two OutFiles
 
Similar Threads
odd n even numbers program
Getting variables from Outfiles
unable to exit program..
getting no such element exception
Totally Confused