Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Default Need help totaling up costs of services from a read in file please

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    
    
    
    public class logBook
    {
       public static void main(String[] args) throws FileNotFoundException
          
          {
          File inputFile = new File("C:\\Users\\Nick Tate\\Desktop\\Log.txt");   
          Scanner in = new Scanner(inputFile);          
             try{
                    PrintWriter out = new PrintWriter("C:\\Users\\Nick Tate\\Desktop\\OutputLog.Txt");
                     double totalDinner = 0;
                     double totalLodging = 0;
                     double totalConference = 0;
                while(in.hasNextLine())
                   {    
                     in.useDelimiter(";");
                     String clientName = in.next();
                     String serviceCost = in.next();
                     Double cost = Double.parseDouble (serviceCost.trim() );
                     String serviceSold = in.next();
                     if(serviceSold.equals("Dinner"))
                     {
                    
                      totalDinner += cost;
                      System.out.print(totalDinner);
                     }
                     else if(serviceSold.equals("Lodging"))
                     {
                      
                       totalLodging += cost;
                      System.out.print(totalLodging);
                     }
                    else if(serviceSold.equals("Conference"))
                     {
                      
                      totalConference += cost;
                      System.out.print(totalConference);
                     }
                    
                     String serviceDate = in.next();
                     in.nextLine();
            
                     out.println(clientName);
                     out.println(cost);
                     out.println(serviceSold);
                     out.println(serviceDate);
                    
                }
                
                out.close();
                 in.close();
                 }
                 catch(FileNotFoundException e)
                 {
                   e.printStackTrace();
                 }  
                
                   
               
                
          }
    }
    So far this is the code I have. It will successfully read the contents of the file(which are below in quotations) and write them to the designated file. My issue is totaling the corresponding costs to their services and then writing that to the file. The if and else if clauses above do not work but how is it that I can get them working? Any help would be appreciated, thank you.
    "John; 67.00; Dinner ; Aug 12 2013;
    Bob; 200.00; Conference; Sep 11 2013;
    Clara; 450.00; Lodging; Oct 25 2013;
    Jamie; 450.00; Lodging; Oct 28 2013;
    Rachel; 67.00; Dinner; Nov 11 2013;
    Richard; 200.00; Conference; Dec 17 2013;
    Nick; 67.00; Dinner; Jan 05 2014;"

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need help totaling up costs of services from a read in file please

    The fact that the System.out.print(...) calls in the if blocks are not printing out anything should give you a big clue on the problem. Just add a println statement for serviceSold, and it should become quite obvious to you, e.g.,
    Java Code:
    ...
    String serviceSold = in.next();
    System.out.println("serviceSold: [" + serviceSold + "]");
    ...

Similar Threads

  1. Replies: 2
    Last Post: 12-31-2012, 12:41 PM
  2. Calculating segment costs
    By sezzy in forum New To Java
    Replies: 4
    Last Post: 11-16-2012, 02:32 PM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •