检查输入格式

时间:2014-03-27 19:57:53

标签: java

我有一个我正在为课堂写作的课程,而且我最后一部分都被困在了。这是它要求的内容,以及我坚持的内容:

  

如果文件不存在或格式不正确,则显示错误   不正确。

输入的格式如下:

  

名称;服务;价格;日期

     

     

Bob Smith;晚餐; 52.35; 04-01-2014

到目前为止我的代码:

package school;
import java.util.*;
import java.io.*;
public class HotelSales{

    public static void main(String[] args){
        try {
            BufferedReader br = new BufferedReader(new FileReader("input.txt"));
            // I assume the format check would go here?
            String[] array = new String[48];
            double conferenceTotal = 0;
            double dinnerTotal = 0;
            double lodgingTotal = 0;
            String line = "";
            while((line = br.readLine()) != null){
                array = line.split(";");
                if(array[1].equals("Conference")) {
                    conferenceTotal += Double.parseDouble(array[2]);
                } else if(array[1].equals("Dinner")) {
                    dinnerTotal += Double.parseDouble(array[2]);
                } else if(array[1].equals("Lodging")) {
                    lodgingTotal += Double.parseDouble(array[2]);
                }
            }
            System.out.println("The totals for the sales are: \n");
            System.out.printf("Conference Total: $%-5.2f\n", conferenceTotal);
            System.out.printf("Dinner Total: $%-5.2f\n", dinnerTotal);
            System.out.printf("Lodging Total: $%-5.2f\n", lodgingTotal);

            BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
            bw.write("The totals for the sales are: ");
            bw.newLine();
            bw.newLine();
            bw.write("Conference Total: $" + String.format("%-5.2f",conferenceTotal));
            bw.newLine();
            bw.write("Dinner Total: $" + String.format("%-5.2f",dinnerTotal));
            bw.newLine();
            bw.write("Lodging Total: $" + String.format("%-5.2f",lodgingTotal));

            br.close();
            bw.close();

        } catch (InputMismatchException e) { //And that this is the proper catch right?
            System.out.print("Wrong input file format.\n");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.print("Sorry, the file was not found.\n");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.print("Oops! I/O Exception.\n");
            e.printStackTrace();
        }
    }
}

谢谢! :)

2 个答案:

答案 0 :(得分:0)

这应该有效:

    while((line = br.readLine()) != null){
        String [] array = line.split(";");
        if ( array.length != 4 ) {
            throw new InputMismatchException( "Invalid ... blah blah, expected 4 elements, found " + array.length );
        }
        try {
            if(array[1].equals("Conference")) {
                conferenceTotal += Double.parseDouble(array[2]);
            } else if(array[1].equals("Dinner")) {
                dinnerTotal += Double.parseDouble(array[2]);
            } else if(array[1].equals("Lodging")) {
                lodgingTotal += Double.parseDouble(array[2]);
            }
        } catch ( NumberFormatException nfe ) {
            throw new InputMismatchException( nfe );
        }
    }

并删除该行:

String[] array = new String[48];

答案 1 :(得分:0)

如果您的输入文件没有用;分隔,那么当您尝试Double.parseDouble(array [2])时,您将得到一个ArrayIndexOutOfBoundsException或NullPointerException,因为您的数组大小为1。

while ((line = br.readLine()) != null)
{
    array = line.split(";");
    if (array.length != 4)
    {
        throw new InputMismatchException("Invalid ... blah blah, expected 4 elements, found " + array.length);
    }
    try
    {
        if (array[1].equals("Conference"))
        {
            conferenceTotal += Double.parseDouble(array[2]);
        }
        else if (array[1].equals("Dinner"))
        {
            dinnerTotal += Double.parseDouble(array[2]);
        }
        else if (array[1].equals("Lodging"))
        {
            lodgingTotal += Double.parseDouble(array[2]);
        }
    }
    catch (ArrayIndexOutOfBoundsExceptione aioobe)
    {
        throw new InputMismatchException(aioobe);
    }
相关问题