0

我想要做的是读取一个文本文件,其中包含

account_number time_of_call 分钟 10011 20:00 10.2 10011 13:23 12.3 10033 01:00 34.0

我想用这个向客户收费

每个帐户每月 10 美元的基本费用
上午 8:00 至晚上 10:00 之间开始的通话每分钟 0.10 美元,包括
所有其他时间每分钟 0.05 美元

我已经得到了答案和我想把它放进去的格式,但我得到了一个 java.util.NoSuchElementException ,我似乎无法弄清楚。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.Vector;


public class PhoneBill {
    Vector data;
    Vector processed = new Vector();

    Vector markProcessed = new Vector();
    public void readFile(String inFileStr)
    {
        String str = "";
        data = new Vector<LineItem>();
        FileReader fReader;
        InputStream inFileStream;
         try{
        fReader = new FileReader(inFileStr);
            BufferedReader br=new BufferedReader(fReader);
            String line;
            while ((line=br.readLine())!= null){
                if (line.indexOf("_") != -1)
                    continue;
                else
                  if (!line.isEmpty()){
                    data.add(new LineItem(line.trim()));

                  }
            }

            br.close(); 
        }       
        catch (Exception e){
            System.out.println(e.toString());
        }
    }
    public void processCharges()
    {
        System.out.println ("Invoice");
        System.out.println ("--------------------------");
        System.out.println ("Account   " + "Amount Due ");
        double total = 0.0;
        double lCharges =0;
        boolean done = false;
        for (int j = 0; j < data.size(); j++ ){
            LineItem li =  (LineItem)data.get(j);
            String accNum = li.getAccountNum();
            if (j > 0){
                done = checkProcessed(accNum);}
            else
                processed.add(accNum);
            if (!done){
                   lCharges = 0;
            for (int i = 0; i < data.size(); i++){
              String acc = ((LineItem)data.get(i)).getAccountNum();
              if (acc.equals(accNum) && !done)
              lCharges += processItemCharges(accNum);
              done = checkProcessed(accNum);
            }
            lCharges+=10.0;
            System.out.format ("%s" + "      $%.2f%n",accNum, lCharges);
            processed.add(accNum);
             total += lCharges;
            }

        }

        System.out.println ("--------------------------");
        System.out.format ("%s" + "      $%.2f%n","Total", total);
    }
    private boolean checkProcessed(String accNum){
        if (processed.contains(accNum))
            return true;
        else
            return false;
    }


    private double processItemCharges(String accNum)
    {
        double charges = 0.0;

        for (int i = 0; i < data.size(); i++)
        {
            if(((LineItem)data.get(i)).getAccountNum().equals(accNum))
                charges += ((LineItem)data.get(i)).getCharges();
        }
        return charges;
    }
    public static void main(String[] args)
    {
        PhoneBill pB =  new PhoneBill();
        pB.readFile("input_data.txt");
        pB.processCharges();
    }

    class LineItem{
        String accNum ;
        String timeOfCall;
        double mins;
        double amountDue;
        boolean counted = false;

        public LineItem(String accStr)
        {
            processAccount(accStr);
        }

        private void processAccount(String accStr){
            StringTokenizer st = new StringTokenizer(accStr);
            accNum = (String)st.nextElement();
            timeOfCall = (String) st.nextElement();
            mins = Double.parseDouble((String) st.nextElement());
            if (timeOfCall.compareTo("08:00")>0 && timeOfCall.compareTo("22:00")<0)
                amountDue = mins*0.10;
            else
                amountDue = mins*0.05;
        }

        public String getAccountNum()
        {
            return accNum;
        }

        public double getCharges()
        {
            return amountDue;
        }

    }
}
4

1 回答 1

0

您没有提供太多帮助,因为您没有提供生成异常的代码行。顺便说一句,我敢打赌,您的异常是从其中一个调用中引发的

st.nextElement()

好的,我知道这对您没有太大帮助;我认为这对您了解在开发的早期阶段处理异常的最佳方法是调用:

e.printStackTrace();

代替

System.out.println(e.toString());

我希望这个建议可以帮助你。

于 2013-09-30T17:45:35.113 回答