Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java program that reads a file

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    Example of the File

    Sue Smith
    7 6.5
    8.0 25.0 12.5 14.0 15.0 15.0 20.0 10.0 -1
    John Edward Doe
    0 5.0
    -1
    George W Jones IV
    0 0
    13.5 8.5 10.5 -1


    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.

    Example Output format

    Customer: Sue Smith
    Tree Removal: $3500.00
    Tree Trimming: $1200.00
    Stump Grinding: $525.00
    Total: $5225.00

    Customer: John Edward Doe
    Tree Removal: $0.0
    Tree Trimming: $500.00
    Stump Grinding: $0.00
    Total: $500.00

    Total Revenue $5725.00


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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.

    Much luck!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    **/

    public class Prog5
    {
    public static void main(String[] args) throws FileNotFoundException
    {
    Scanner in= new Scanner(System.in);
    File file = new File("prog5.dat");

    while(in.hasNextLine())
    {
    String customer = in.nextLine();
    System.out.print(customer);
    }
    in.close();
    }

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    File inputfile = new File("prog5.dat");
    Scanner in= new Scanner(inputfile);


    while(in.hasNext())
    {
    customer = in.next();
    treeRemoval = in.next();
    stumpGrinding = in.next();
    System.out.print(customer);
    System.out.println(treeRemoval);
    System.out.println(stumpGrinding);
    }

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Java program that reads a file

    Your scanner is reading from System.in, where it should be reading from the file.

    Try and use NIO. The earlier stuff is classed as "legacy i/o".

    http://docs.oracle.com/javase/tutori...io/fileio.html

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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"
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Reading a File

    Java program that reads a file
    Duplicate

Similar Threads

  1. Replies: 11
    Last Post: March 10th, 2013, 07:40 PM
  2. Write a Java program that reads file
    By Mehwish-S in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 28th, 2012, 03:00 PM
  3. Replies: 1
    Last Post: May 2nd, 2012, 08:06 AM
  4. Creating a program that reads from a file
    By Twoacross in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 20th, 2011, 09:19 PM
  5. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM