The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Java program that reads a file

[code=Java]
I have to compute a program that calculates charges for several customers.
Tree removal $250.00 per tree
Tree trimming $40.00 per hour
Stump grinding $25.00 plus $2.00 per inch for each stump shose diameter exceeds ten minutes.

First line contains the name of the customer. Second line contains the number of trees to be removed followed by hours for tree trimming. Third line, if there are stumps, contains the diameters of the stumps terminated by -1. After the last customer is read and printed, the program should print the total revunue.

Re: Java program that reads a file

What you've done is to post your entire assignment without showing what work you've done, without asking a specific question, and that is not how to get help here (or at any forum). For one, we have no idea what you're having trouble with, so we won't know how to advise you. For another, if you don't at least try to solve what you can first, you're cheating yourself out a valuable part of your learning experience, and finally it suggests that you want someone to post a complete solution, thereby doing your homework for you, and I know that this was not your intent.

So please try to solve as much of the problem that you can first, then post what you've done using code tags (see my signature), and then ask a specific question. Do this and you'll have many folks willing to help you.

Re: Java program that reads a file

Ok I'll send what i have. I just want it to read the file and output the customer's name. There's no output in the console
[Code=Java}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
Programmer: James DeSaussure
CSCI 140L
Program 5
Last Revised: March 24, 2013
Compute a program to allow the manager to calculate the charges for several customers.
**/

Reading a File

So this is how the FIle I need to read looks.

Sue Smith
7 6.5
8.0 25.0

The First line is the name.
Second line is the The number of trees followed by the hours
THe third line is the diameters.
I can not figure out how to place the numbers on the secong line in two different variables.
This is the piece of code where I am confused.
[Code=Java]
public static void main(String[] args) throws FileNotFoundException
{
String customer;
String treeRemoval;
String treeTrimming;
String stumpGrinding;

Re: Reading a File

Read the lines from the file using nextLine() and then break the contents of the line into its parts as needed. One way to get the separate parts from the line is to use the Scanner class's constructor that takes a String.

Using the next() method will be a problem if the name on the first line has more than one token like the example you posted. next() would read "Sue" and leave "Smith"