Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java文件IO读取下一行并创建一个要显示的总数_Java_Io - Fatal编程技术网

Java文件IO读取下一行并创建一个要显示的总数

Java文件IO读取下一行并创建一个要显示的总数,java,io,Java,Io,我在试图找出如何从上到下读取文本文件时遇到了问题。我有一个名为“Tim”的文本文件,扩展名为.text,内容为: 我想在名字里读一读,然后是销售(数字)。我需要添加每个销售并创建一个总数。我可以在格式为“Tim 5000 5000”时添加销售额,但不能在从上到下添加销售额。总数为零 public static void main (String [] args) { //local constants final String QUIT = ("-1"

我在试图找出如何从上到下读取文本文件时遇到了问题。我有一个名为“Tim”的文本文件,扩展名为.text,内容为:

我想在名字里读一读,然后是销售(数字)。我需要添加每个销售并创建一个总数。我可以在格式为“Tim 5000 5000”时添加销售额,但不能在从上到下添加销售额。总数为零

    public static void main (String [] args)
{
    //local constants
    final String QUIT = ("-1"); // Sets the Quit value
    final int MAX = 10;

    //local variables
    Scanner input = new Scanner(System.in); // Creates an object of Scanner
    String fileName;
    FileReader inFile;
    BufferedReader bFile;
    BufferedWriter w;
    FileWriter fw;
    int sales;
    String array[];
    int count;
    String sentence;
    String report = "";
    StringTokenizer line;

    /********************   Start main method  *****************/

    // Spacing
    System.out.print ("\n\n\n\n");

    System.out.print (Util.setLeft(17, "Enter just the file name without extension or quit (-1): "));
    fileName = input.nextLine();

    while (!fileName.equals (QUIT))
    {
        try
        {
            //Open the input stream for reading
            inFile = new FileReader(fileName + ".Text");
            bFile = new BufferedReader (inFile);

            //Open the output file for writing
            fw = new FileWriter(fileName + " " + "Sales Report.Text");
            w = new BufferedWriter (fw);

            // Instantiate the array
            array = new String[MAX];

            // initalize count to zero
            count = 0;

            //get first line of input file
            sentence = bFile.readLine();

            //while their are more lines in the input file
            while (sentence != null)
            {
                report = readFile (fileName, sentence, bFile);

                array[count] = report;

                count++;

                // Write line to file
                w.write(report);

                //get next line from the input file
                sentence = bFile.readLine();

            } // END WHILE

            for (int i = 0; i < count; i++)
            {

                System.out.println (array[i]);

            }

            //write the last line to the output file, flush and close buffer
            w.flush ();
            w.close ();

            // Spacing
            System.out.print ("\n\n\n\n");

            // Display message for file being created
            System.out.print (Util.setLeft(20,"************************************************\n"));
            System.out.print (Util.setLeft(20,"*") + Util.setLeft(46, "*\n"));
            System.out.print (Util.setLeft(20,"*") + Util.setLeft(3,"File has been created: " + fileName + " " + "Formated") + Util.setLeft(3, "*\n"));
            System.out.print (Util.setLeft(20,"*") + Util.setLeft(46, "*\n"));
            System.out.print (Util.setLeft(20,"************************************************\n\n"));

        }// END TRY

        //Exception for no file found
        catch(IOException exception)
        {

            // Spacing
            System.out.print ("\n\n\n\n");

            // Display message for file being created
            System.out.print (Util.setLeft(20,"*************************************************\n"));
            System.out.print (Util.setLeft(20,"\n") + Util.setLeft(32,"Couldn't find file - " + fileName)
                                                    + Util.setLeft(10, "\n\n"));
            System.out.print (Util.setLeft(20,"*************************************************\n\n"));

            //System.out.println(exception);
            exception.printStackTrace();

            // Spacing
            System.out.print ("\n\n\n\n");

        }

        System.out.print (Util.setLeft(17, "Enter just the file name without extension or quit (-1): "));
        fileName = input.nextLine();

    } // END WHILE

    // Call function to display closing message
    systemQuit();

}

public static String readFile(String fileName, String sentence, BufferedReader bFile) throws IOException
  {

  //local constants

  //local variables
  String tempReport = "";
  StringTokenizer line;
  String employeeName = "";
  String lName = "";
  String fullName;
  String sales;
  int total = 0;

  /******************************************************/

  //use string tokenizer to parse each line in the file
  line = new StringTokenizer(sentence);

  //get the name first
  employeeName = line.nextToken();

  //while there are more words in this line
  while(line.hasMoreTokens())
  {
     //get next line from the input file
     sales = bFile.readLine();

     total = Integer.parseInt(sales);

     total = total + total;

  }

  // Converts Course Grade to a String
     String strSales = Float.toString(total);

    tempReport = ((Util.setLeft(10, employeeName) + (Util.setLeft(28 - employeeName.length(), strSales) + "\n")));

  // Return
  return(tempReport);
}

public static void systemQuit()
{

    //local constants

    //local variables

    /******************************************************/

    // Spacing
    System.out.print ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

    // Display message for user quit the program
    System.out.print (Util.setLeft(20,"*************************************************\n"));
    System.out.print (Util.setLeft(20,"*") + Util.setLeft(47, "*\n"));
    System.out.print (Util.setLeft(20,"*") + Util.setLeft(15,"You Quit the Program") + Util.setLeft(12, "*\n"));
    System.out.print (Util.setLeft(20,"*") + Util.setLeft(47, "*\n"));
    System.out.print (Util.setLeft(20,"*************************************************\n\n"));

    // Spacing
    System.out.print ("\n\n\n\n\n\n\n\n\n\n\n\n\n");
 }