Im kinda still new at java and with help, developed a java file that will take numbers from a text file and calculate a invoice that will format like this:

Invoice
--------------------------
Account   Amount Due 
10011      $12.25
10033      $11.70
--------------------------
Total      $23.95

I got it to print out to console and the last thing I need to do next is create a "invoice.txt" textfile that will write the output + the format of it. I know how to use printerwriter and fileoutput stream to put create text from inputs but not sure how to do the output of a program. Can someone help me with my code to create a method that will print the output onto text? How am I suppose to set it up with my following code?

            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) throws Exception
                {
                    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;
                    }

                }
            }

I also have a java.util.NoSuchElementException with no idication which line its on.

Instead of System.out.println use yourPrintWriter.println (etc)

replace System.out.println(e.toString()); with e.printStackTrace(); to see the calling sequence and line number when the exception was thrown

thanks when I did that I got this

java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at java.util.StringTokenizer.nextElement(Unknown Source)
    at ShortProjects.PhoneBill$LineItem.processAccount(PhoneBill.java:115)
    at ShortProjects.PhoneBill$LineItem.<init>(PhoneBill.java:110)
    at ShortProjects.PhoneBill.readFile(PhoneBill.java:31)
    at ShortProjects.PhoneBill.main(PhoneBill.java:96)

So look at the code on line 115 of your PhoneBill code (processAccount method) because that's where you are trying to access a next element that doesn't exist. Add some print staements to see exactly what the variables / data are at that point on the program.

ok i fixed the error and i figured out how to print the file... but im having trouble creating the file with the same output as the console.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
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){

        }
    }
    public void processCharges()
    {
        String out = "Account Amount_Due\n";
        System.out.println ("Invoice");
        System.out.println ("--------------------------");
        System.out.println ("Account   " + "Amount Due ");
        double total = 0.0;
        double lCharges =0;
        boolean done = false;
        DecimalFormat numFormatter = new DecimalFormat("$##.##");
        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);
            out += accNum+" ";
            out += numFormatter.format(lCharges)+"\n";
            processed.add(accNum);

             total += lCharges;
            }

        }

        System.out.println ("--------------------------");
        System.out.format ("%s" + "      $%.2f%n","Total", total);
        writeToFile("invoice.txt", out);

    }
    private void writeToFile(String filename,String outStr)
    {
        try{
            File file = new File(filename);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(outStr);
            bw.close();

        } catch (IOException ioe){
            System.out.println(ioe.getMessage());
        }

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

    }
}

another (yet smaller) remark:

private boolean checkProcessed(String accNum){
                    if (processed.contains(accNum))
                        return true;
                    else
                        return false;
                }

is basically the same as:

private boolean checkProcessed(String accNum){
                    return processed.contains(accNum);
                }

it's not really necessary to create a separate method for this, since in order to 'write less code' you actually write more, while 'processed.contains(accNum)' is still pretty readable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.