如何使用文本文件中的PrinterWriter类实现以下结果? [英] How do I achieve the following results using the PrinterWriter class from a text file?

查看:114
本文介绍了如何使用文本文件中的PrinterWriter类实现以下结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处我的应用程序会提示用户输入 mixed.txt 文本文件,其中包含

My application here prompts the user for a text file, mixed.txt which contains

12.2 Andrew
22 Simon
Sophie 33.33
10 Fred
21.21 Hank
Candice 12.2222

接下来,应用程序将 PrintWrite 添加到所有文本文件,即 result.txt errorlog.txt 。 mixed.txt中的每一行都应以数字开头,后跟名称。但是,某些行可能包含另一种方式来表示名称,然后是数字。以数字开头的那些应添加到 sum 变量并写入result.txt文件,而那些以名称开头的行和数字应写入errorlog.txt文件。

Next, the application is to PrintWrite to all text files namely result.txt and errorlog.txt. Each line from mixed.txt should begin with a number first followed by a name. However, certain lines may contain the other way round meaning to say name then followed by a number. Those which begins with a number shall be added to a sum variable and written to the result.txt file while those lines which begin with the name along with the number shall be written to the errorlog.txt file.

因此,在MS-DOS控制台上,结果如下:

Therefore, on the MS-DOS console the results are as follow:


输入结果.txt

type result.txt

总计:65.41


类型errorlog.txt

type errorlog.txt

第3行出错 - Sophie 33.33

第6行出错 - Candice 12.2222

好的,这是我的问题。我只是设法进入舞台,我已将所有数字添加到result.txt和errorlog.txt文件的名称,我不知道如何继续从那里开始。那么你们能给我一些建议或帮助我们如何达到我需要的结果吗?

Ok here's my problem. I only managed to get up to the stage whereby I have had all numbers added to result.txt and names to errorlog.txt files and I have no idea how to continue from there onwards. So could you guys give me some advice or help on how to achieve the results I need?

以下是我的代码:

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

class FileReadingExercise3 {

public static void main(String[] args) throws FileNotFoundException
{
    Scanner userInput = new Scanner(System.in);
    Scanner fileInput = null;
    String a = null;
    int sum = 0;

     do {
        try
        {
            System.out.println("Please enter the name of a file or type QUIT to finish");
            a = userInput.nextLine();
            if (a.equals("QUIT"))
            {
                System.exit(0);
            }

            fileInput = new Scanner(new File(a));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error " + a + " does not exist.");
        }
    } while (fileInput == null);

    PrintWriter output = null;
    PrintWriter output2 = null;

    try
    {
        output = new PrintWriter(new File("result.txt"));           //writes all double values to the file
        output2 = new PrintWriter(new File("errorlog.txt"));        //writes all string values to the file
    }
    catch (IOException g)
    {
        System.out.println("Error");
        System.exit(0);
    }

    while (fileInput.hasNext()) 
    {
        if (fileInput.hasNextDouble())
        {
            double num = fileInput.nextDouble();
            String str = Double.toString(num);
            output.println(str);
        } else
        {
            output2.println(fileInput.next());
            fileInput.next();
        }
    }

    fileInput.close();
    output.close();
    output2.close();
}
}

这是mixed.txt文件的屏幕截图:

This is the screenshot of the mixed.txt file:

推荐答案

您可以像这样更改您的while循环:

You can change your while loop like this:

    int lineNumber = 1;

    while (fileInput.hasNextLine()) {
        String line = fileInput.nextLine();
        String[] data = line.split(" ");
        try {
            sum+= Double.valueOf(data[0]);
        } catch (Exception ex) {
            output2.println("Error at line "+lineNumber+ " - "+line);
        }
        lineNumber++;
    }
    output.println("Total: "+sum);

在这里你可以浏览 mixed.txt 并检查它是否以双精度开头。如果它是double,你可以将它添加到 sum ,否则你可以将String添加到errorlog.txt。最后你可以将总和添加到result.txt

Here you can go through each line of the mixed.txt and check if it starts with a double or not. If it is double you can just add it to sum or else you can add the String to errorlog.txt. Finaly you can add the sum to result.txt

这篇关于如何使用文本文件中的PrinterWriter类实现以下结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆