| | |
Writing my program output to a text file.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Can somebody please help me with this code? I have the program correctly coded and operating properly, however, I forget how to write my output to a text file. Can somebody please help me here? Thanks.
Java Syntax (Toggle Plain Text)
/** * This program calculates the monthly Heat Index for a specific city. * * @author John D. Barry * @date January 30, 2009 */ import java.util.Scanner; import java.io.IOException; import java.io.PrintWriter; import java.io.File; class HeatIndex { public static void main (String [ ] args) throws IOException { int index = 0; double [] theTemps = new double[13]; File temps = new File("KeyWestTemp.txt"); Scanner inFile = new Scanner(temps); while (inFile.hasNextDouble()) { theTemps[index] = inFile.nextDouble(); index++; } for (double d: theTemps) { } inFile.close(); System.out.println(); System.out.println(); index = 0; double [] theHumidity = new double[12]; File humid = new File("KeyWestHumid.txt"); inFile = new Scanner(humid); while (inFile.hasNextDouble()) { theHumidity[index] = inFile.nextDouble(); index++; } for (double d: theHumidity) { } int sumOne = 0; for (int i = 0; i < theHumidity.length; i++) { sumOne += theHumidity[i]; } double averageOne = sumOne / 12; double fourthHI = (-42.379) + (2.04901523 * theTemps[4]) + (10.14333127 * theHumidity[4]) - (0.22475541 * theTemps[4] * theHumidity[4]) - (6.83783E-3 * theTemps[4] * theTemps[4]) - (5.481717E-2 * theHumidity[4] * theHumidity[4])+ (1.22874E-3 * theTemps[4] * theTemps[4] * theHumidity[4]) + (8.5282E-4 * theTemps[4] * theHumidity[4] * theHumidity[4]) - (1.99E-6 * theTemps[4] * theTemps[4] * theHumidity[4] * theHumidity[4]); double fifthHI = (-42.379) + (2.04901523 * theTemps[5]) + (10.14333127 * theHumidity[5]) - (0.22475541 * theTemps[5] * theHumidity[5]) - (6.83783E-3 * theTemps[5] * theTemps[5]) - (5.481717E-2 * theHumidity[5] * theHumidity[5])+ (1.22874E-3 * theTemps[5] * theTemps[5] * theHumidity[5]) + (8.5282E-4 * theTemps[5] * theHumidity[5] * theHumidity[5]) - (1.99E-6 * theTemps[5] * theTemps[5] * theHumidity[5] * theHumidity[5]); double sixthHI = (-42.379) + (2.04901523 * theTemps[6]) + (10.14333127 * theHumidity[6]) - (0.22475541 * theTemps[6] * theHumidity[6]) - (6.83783E-3 * theTemps[6] * theTemps[6]) - (5.481717E-2 * theHumidity[6] * theHumidity[6])+ (1.22874E-3 * theTemps[6] * theTemps[6] * theHumidity[6]) + (8.5282E-4 * theTemps[6] * theHumidity[6] * theHumidity[6]) - (1.99E-6 * theTemps[6] * theTemps[6] * theHumidity[6] * theHumidity[6]); double seventhHI = (-42.379) + (2.04901523 * theTemps[7]) + (10.14333127 * theHumidity[7]) - (0.22475541 * theTemps[7] * theHumidity[7]) - (6.83783E-3 * theTemps[7] * theTemps[7]) - (5.481717E-2 * theHumidity[7] * theHumidity[7])+ (1.22874E-3 * theTemps[7] * theTemps[7] * theHumidity[7]) + (8.5282E-4 * theTemps[7] * theHumidity[7] * theHumidity[7]) - (1.99E-6 * theTemps[7] * theTemps[7] * theHumidity[7] * theHumidity[7]); double eighthHI = (-42.379) + (2.04901523 * theTemps[8]) + (10.14333127 * theHumidity[8]) - (0.22475541 * theTemps[8] * theHumidity[8]) - (6.83783E-3 * theTemps[8] * theTemps[8]) - (5.481717E-2 * theHumidity[8] * theHumidity[8])+ (1.22874E-3 * theTemps[8] * theTemps[8] * theHumidity[8]) + (8.5282E-4 * theTemps[8] * theHumidity[8] * theHumidity[8]) - (1.99E-6 * theTemps[8] * theTemps[8] * theHumidity[8] * theHumidity[8]); double ninthHI = (-42.379) + (2.04901523 * theTemps[9]) + (10.14333127 * theHumidity[9]) - (0.22475541 * theTemps[9] * theHumidity[9]) - (6.83783E-3 * theTemps[9] * theTemps[9]) - (5.481717E-2 * theHumidity[9] * theHumidity[9])+ (1.22874E-3 * theTemps[9] * theTemps[9] * theHumidity[9]) + (8.5282E-4 * theTemps[9] * theHumidity[9] * theHumidity[9]) - (1.99E-6 * theTemps[9] * theTemps[9] * theHumidity[9] * theHumidity[9]); double sumTwo = theTemps[0] + theTemps[1] + theTemps[2] + theTemps[3] + fourthHI + fifthHI + sixthHI + seventhHI + eighthHI + ninthHI + theTemps[10] + theTemps[11]; double averageTwo = sumTwo / 12; System.out.println(" Heat Index: Key West, Florida"); System.out.println(); System.out.println(" Months"); System.out.println(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Avg "); System.out.println("***************************************************************************************************************************"); System.out.printf("Temp (F)%18.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f", theTemps[0],theTemps[1],theTemps[2],theTemps[3],theTemps[4],theTemps[5],theTemps[6],theTemps[7],theTemps[8],theTemps[9],theTemps[10],theTemps[11],theTemps[12]); System.out.println(); System.out.printf("Humidity %17.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f", theHumidity[0],theHumidity[1],theHumidity[2],theHumidity[3],theHumidity[4],theHumidity[5],theHumidity[6],theHumidity[7],theHumidity[8],theHumidity[9],theHumidity[10],theHumidity[11],averageOne); System.out.println(); System.out.printf("HI (F)%20.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f%8.1f",theTemps[0],theTemps[1],theTemps[2],theTemps[3],fourthHI,fifthHI,sixthHI,seventhHI,eighthHI,ninthHI,theTemps[10],theTemps[11],averageTwo); System.out.println(); inFile.close(); } }
Java Syntax (Toggle Plain Text)
BufferedWriter writer = new BufferedWriter(new FileWriter("fileName")); writer.write("write here"); writer.newLine();
Check the API for the BufferedWriter in case I have some errors in the syntax
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- How to read text file and display it in a messagebox (ASP.NET)
- text file - stream object issues (C++)
- printing text file string contents (array) - padded characters (C++)
- Difference between binary and text file streams (C++)
- Reading 2 diffrent input files and outputting to 2 different output files (C++)
- Encrypting and Decrypting text from file using keyword (C)
- C++ Reading from a text file (C++)
- I still need help with my program!! (Java)
- Error trying to write to existing file (C)
Other Threads in the Java Forum
- Previous Thread: jTDS Driver Supports IPV6?
- Next Thread: Reading seperate text file columns into seperate arrays
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows xstream






