使用足球赛季文件中的分数读取.txt,然后使用团队记录打印另一个.txt

时间:2015-12-11 19:50:13

标签: java

我正在尝试编写一个读取文件的程序" soccer.txt"有以下信息:

标题## soccer.txt:

Lions Panthers Tigers Sky
Panthers    4   Tigers      3
Tigers      3   Lions       1
Sky         2   Panthers    0
Lions       2   Tigers      1
Tigers      1   Sky         6
Sky         2   Panthers    1
Tigers      1   Sky         4
Panthers    3   Tigers      6
Lions       3   Sky         2
Tigers      4   Sky         3
Sky         2   Panthers    1
Lions       4   Panthers    6

然后输出.txt文件:

标题## results.txt:

Team        Wins        Losses
Lions       2            2
Panthers    2            4  
Sky         5            2
Tigers      3            4

我已经开始为此编写代码了。我正在尝试测试它,但我不能让程序重新认识到.txt文件存在,即使我有" soccer.txt"文件已保存到桌面。

import java.io.*;
import java.util.*;

public class Soccer
{
  public static void main (String[] args) throws IOException
  {
    //initiate an input file object and scanner object
    File inData = new File("soccer.txt");
    if(!inData.exists())
    {
      System.out.println("File does not exist");
      System.exit(0);
    }
    Scanner input = new Scanner(inData);

    //create output file and a printWriter object
    File outData = new File("results.text");
    PrintWriter output = new PrintWriter(outData);


    String teamName1 = input.next();
    String teamName2 = input.next();
    String teamName3 = input.next();
    String teamName4 = input.next();

    output.println(teamName1);
    output.println(teamName2);
    output.println(teamName3);
    output.println(teamName4);

    input.close();
    output.close();
  }
}

0 个答案:

没有答案