2014-03-27 71 views
-1

我有一个课程正在写给班级,而且我被困在最后一部分。下面是它要求,什么我被困在:检查输入格式

显示一个错误,如果该文件不存在或者格式不正确 。

输入的格式化是沿着线:

名称;服务;价格;日期

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(); 
     } 
    } 
} 

谢谢! :)

+0

有啥牛逼他有问题吗?您似乎在捕捉异常并为它们打印合理的错误消息。 – Mureinik

+0

我不知道它是否真的捕捉到我想要的。我会假设它会抛出我的InputMismatchException并显示“错误的输入文件格式。\ n”,但它没有显示出来。 – MikeN

+0

我想你错过了'NumberFormatException'的一个捕获 – Mureinik

回答

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); 
     } 
    } 

,并删除这一行:;的,那么当你试图传递给Double.parseDouble(

String[] array = new String[48]; 
0

如果你输入的文件不被分离数组[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); 
    }