Results 1 to 9 of 9
  1. #1
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default File IO (PrintWriter and Methods)

    Taking a Java Class, and having a hard time completing this assignment and need help. The program is suppose to read a file, compute the numbers from the the .txt file, create an output file and print the numbers onto the output file with the calculations using the numbers from the original file. We have not yet come to the arrays chapter in the book. Any and all help will be appreciated.

    PS The professor provided a template for us to get started, I will post the template, the code I already have, and the input file as an attachment.

    import java.util.Scanner;
    import java.io.*;
    import java.text.*;


    public class FileIO
    {
    public static void main(String[] args)throws IOException
    {
    String name;
    double totalHours = 0;
    double totalGrand;

    Scanner dataIn;
    Scanner kb = new Scanner(System.in);
    File myFile = new File("/FileIO_Input.txt");
    Scanner inputFile = new Scanner(myFile);

    printHeading();

    System.out.print("\n");

    while(inputFile.hasNext())
    {
    int hours = inputFile.nextInt();
    double rate = inputFile.nextDouble();
    name = inputFile.next();
    System.out.print(name + "\t\t" + hours + "\t" + rate);
    System.out.print("\t");
    calculatePay(rate, hours);
    }

    System.out.print("TOTALS");
    }

    public static void printHeading()
    {
    System.out.print("NAME\t\t");
    System.out.print("HOURS\t");
    System.out.print("RATE\t");
    System.out.print("TOTAL\t");
    }

    public static void calculatePay(double rate, int hours)
    {
    double totalPay;
    totalPay = hours * rate;
    System.out.println(totalPay);
    }

    public static void writeOut(PrintWriter outFile, String name, int hours, double rate) throws IOException
    {



    }
    }
    Attached Files Attached Files

  2. #2
    angstrem is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    Java Code:
    int hours = inputFile.nextInt();
    double rate = inputFile.nextDouble();
    Aren't that numbers you need to compute? Just initialize another variable where you'll store the result of computations and, well, change it somehow in the while loop.
    By the way, where particularly do you have problems?

  3. #3
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    Total hours needs to be calculated, and there also needs to be a grand total calculated. Not to mention an output file needs to be created with the new information. name, hours, rate, totalHours, and grandTotal written to it.

  4. #4
    angstrem is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    Tell exactly the formulas you need to apply and the format of the output.

  5. #5
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    There aren't any more formulas other than adding all of the hours and totals together. I guess that would be...
    hours += totalHours; and formatting I can figure out myself. I just need help on creating an output file and writing to it using the methods.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: File IO (PrintWriter and Methods)

    I'm stunned that you are doing file IO before arrays.

    Find yourself a good tutorial that has some code examples. Before attempting to complete your assignment make a simple program that writes some output to a file. Test it thoroughly and if you have problems post your code here. When you have it sorted you can then modify it to your needs.

  7. #7
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    I'm having a hard/impossible time bringing the writeOut() method up into the main method. It keeps on saying that my PrintWriter variable hasn't been initalized, and I'm not sure what to = it to.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: File IO (PrintWriter and Methods)

    Hi rgru25,

    You have not declared a PrintWriter variable ouside of the writeOut() method. Once declared then you can initialize it.

    Are you currently calling the writeOut() method? If so, how? Answers to these questions would allow us to better resolve your issue.

    Regards.

  9. #9
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: File IO (PrintWriter and Methods)

    I think you need the output in file format.

    you refer this link:
    How to write to file in Java – FileOutputStream
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

  1. Need help with PrintWriter!
    By PapaEcho in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 07:51 PM
  2. printwriter
    By sope in forum New To Java
    Replies: 2
    Last Post: 05-09-2011, 08:57 PM
  3. Help with printwriter class
    By eel in forum New To Java
    Replies: 9
    Last Post: 09-19-2010, 03:04 PM
  4. Help with PrintWriter code
    By eel in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 08:36 AM
  5. Help with printwriter.
    By Addez in forum New To Java
    Replies: 2
    Last Post: 10-30-2009, 02:58 PM

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
  •