• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with totaling up cost of service categories please  RSS feed

 
charles lincoln
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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;"
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you are doing. Taking a String from the keyboard, splitting it, and taking one of several tokens?
You are all right reading from the keyboard with a Scanner, but you should not then try changing the delimiter. There are several ways to split a String, one with a String method, or you can use a second Scanner taking that String as its input.
Or you can use the methods of the Scanner class which give different data types from String, but what happens when you reach the end of the line? You would have to change the delimiter back.

To find out whether those ifs are working, put a print statement in each. See whether you are getting "Dinner", as opposed to "dinner" or " Dinner". Also copy the equivalence test and get that printed as true or false.

The real object‑oriented way to do it is to create a class which encapsulates the bill, taking a String as its constructor.
 
charles lincoln
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading in list and I need to output the categories and their corresponding sum totals. I do not have a problem with the scanner class, reading a file, or writing a file. My issue is getting an algorithm that will process the values stored in the variable and total them. My code includes a print statement in each if clause however it is not outputting anything. This leads me to believe that the clauses are not being read? How could this happen?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using ; as a delimiter then spaces are going to be included in your tokens. Try trimming them as you did with the cost.
 
charles lincoln
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:If you are using ; as a delimiter then spaces are going to be included in your tokens. Try trimming them as you did with the cost.


Thank you!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!