File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Program using Input file, While loops, decisions (if/else), control break logic, accumulator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program using Input file, While loops, decisions (if/else), control break logic, accumulator" Watch "Program using Input file, While loops, decisions (if/else), control break logic, accumulator" New topic
Author

Program using Input file, While loops, decisions (if/else), control break logic, accumulator

Morgan Grant
Greenhorn

Joined: Jul 29, 2012
Posts: 1
Hello,

I am new here, and to Java (in fact, I'm taking a programming logic course, not even a Java course, so anything I know, I have learned from A Beginner's Guide to Programming Logic and Design (Farrell) and Java Programs to Accompany Programming Logic and Design (Smith). In addition, please feel free to suggest previous topics that answer my questions, links to pages/free resources I can read to learn about beginning Java, etc. I admit that I am coding without really understanding what I'm doing; I look through my books and on the Internet for possible solutions or tips. I am trying to understand, but, well, I need help.

What the program should do:
It should take data (5 lines of integers, with each line having 5 different numbers, white space delimited) from an input file. The 5 numbers on each line represent the from area code, from phone number, to area code, to phone number, and number of minutes the call lasted (in that order). The 5th and final line is just 0s, for the sentinel value.

As long as it doesn't reach a sentinel value of 0 for the "from area code," the program loops, calculating the cost of the call.

Decision portion: the program has to make decisions, depending on the area codes and length of the call, when calculating the cost of a call.

Control break logic portion: If the "to area code" changes, the program should calculate the total cost of the calls to that particular area code (accumulator portion).

The output should look like:

Call Log and Billing Report
===========================
Call from 123 4567890 to 321 7777700 for 25 minutes costs $4.0
Call from 123 4567890 to 321 5555555 for 15 minutes costs $2.0
Total costs for calls to 321 is $6.0

Call from 123 4567890 to 800 2222222 for 10 minutes costs $0.0
Call from 123 4567890 to 800 3333333 for 10 minutes costs $0.0
Total costs for calls to 800 is $0.0

I don't think it matters if the output to the screen happens as the program runs or if it comes only after the program has finished (reached the sentinel value). I would think the former makes more sense, but I'm not sure if one is easier to code.

My questions:
Are lines 39 and 40 necessary?

Line 42: should it be an "if" or a "while"?

There is something wrong with lines 42 and 102 (error says "incompatible types") and 52 and 96 ("int cannot be dereferenced"). Should 0 instead be "null"?



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hi Morgan, welcome to CodeRanch!

Let's start of with the logic portion. Your main loop should look something like this:
Here you can see why it's giving you a type error: You're trying to assign a String value to an int variable. You need to parse it first.

Now that that's out of the way, here's something more important. Your coding style is very monolithic. I expect that you're new to Object Oriented Programming. You should break up your program into more manageable units, it will become much easier for you to reason about it, and it will be much more easier to read.

Have you gone through the Java tutorials yet? They will help you on the way with OO programming. Google for Oracle Java tutorials.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome again
Agree with Stephan that you should break up that main method. In my opinion, a main method contains one statement, even though you see main methods a whole page long in some books.
You also need objects, which need classes to create them from. I disagree with Stephan about the Java Tutorials. They are the most comprehensive source of advice about Java known to modern science, but I don’t think they are very good for teaching the conventions of object‑oriented programming.

I suggest you probably want a PhoneCall class, and PhoneNumber class, and PhoneBill class. Maybe also a PhoneArea class.

Maybe also a PhoneCharges class. If you use that class, it would contain the list of charges as public static final fields (used as constants), a private constructor (to stop anybody creating an instance of that class), and maybe nothing else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program using Input file, While loops, decisions (if/else), control break logic, accumulator