Results 1 to 9 of 9
- 03-13-2013, 10:09 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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
{
}
}
- 03-13-2013, 10:41 PM #2
Member
- Join Date
- Mar 2013
- Posts
- 11
- Rep Power
- 0
Re: File IO (PrintWriter and Methods)
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.Java Code:int hours = inputFile.nextInt(); double rate = inputFile.nextDouble();
By the way, where particularly do you have problems?
- 03-13-2013, 11:06 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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.
- 03-13-2013, 11:14 PM #4
Member
- Join Date
- Mar 2013
- Posts
- 11
- Rep Power
- 0
Re: File IO (PrintWriter and Methods)
Tell exactly the formulas you need to apply and the format of the output.
- 03-14-2013, 01:00 AM #5
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2013, 02:25 AM #6
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.
- 03-14-2013, 08:21 AM #7
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2013, 12:14 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 03-14-2013, 02:38 PM #9
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
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 – FileOutputStreamRegards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
Similar Threads
-
Need help with PrintWriter!
By PapaEcho in forum New To JavaReplies: 2Last Post: 10-09-2011, 06:51 PM -
printwriter
By sope in forum New To JavaReplies: 2Last Post: 05-09-2011, 07:57 PM -
Help with printwriter class
By eel in forum New To JavaReplies: 9Last Post: 09-19-2010, 02:04 PM -
Help with PrintWriter code
By eel in forum New To JavaReplies: 4Last Post: 09-19-2010, 07:36 AM -
Help with printwriter.
By Addez in forum New To JavaReplies: 2Last Post: 10-30-2009, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks