Writing my program output to a text file.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 21
Reputation: jdbarry is an unknown quantity at this point 
Solved Threads: 0
jdbarry's Avatar
jdbarry jdbarry is offline Offline
Newbie Poster

Writing my program output to a text file.

 
0
  #1
Feb 3rd, 2009
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.

  1.  
  2. /**
  3.  * This program calculates the monthly Heat Index for a specific city.
  4.  *
  5.  * @author John D. Barry
  6.  * @date January 30, 2009
  7.  */
  8. import java.util.Scanner;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.io.File;
  12. class HeatIndex
  13.  
  14. {
  15. public static void main (String [ ] args) throws IOException
  16. {
  17.  
  18.  
  19. int index = 0;
  20. double [] theTemps = new double[13];
  21. File temps = new File("KeyWestTemp.txt");
  22. Scanner inFile = new Scanner(temps);
  23.  
  24. while (inFile.hasNextDouble())
  25. {
  26.  
  27. theTemps[index] = inFile.nextDouble();
  28. index++;
  29.  
  30.  
  31. }
  32. for (double d: theTemps)
  33. {
  34.  
  35. }
  36.  
  37.  
  38.  
  39.  
  40. inFile.close();
  41.  
  42. System.out.println();
  43. System.out.println();
  44.  
  45.  
  46. index = 0;
  47. double [] theHumidity = new double[12];
  48. File humid = new File("KeyWestHumid.txt");
  49. inFile = new Scanner(humid);
  50.  
  51. while (inFile.hasNextDouble())
  52. {
  53.  
  54. theHumidity[index] = inFile.nextDouble();
  55. index++;
  56.  
  57.  
  58. }
  59. for (double d: theHumidity)
  60. {
  61. }
  62. int sumOne = 0;
  63. for (int i = 0; i < theHumidity.length; i++)
  64. {
  65. sumOne += theHumidity[i];
  66. }
  67. double averageOne = sumOne / 12;
  68.  
  69.  
  70.  
  71.  
  72. 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]);
  73. 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]);
  74. 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]);
  75. 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]);
  76. 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]);
  77. 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]);
  78.  
  79.  
  80. double sumTwo = theTemps[0] + theTemps[1] + theTemps[2] + theTemps[3] + fourthHI + fifthHI + sixthHI + seventhHI + eighthHI + ninthHI + theTemps[10] + theTemps[11];
  81. double averageTwo = sumTwo / 12;
  82.  
  83. System.out.println(" Heat Index: Key West, Florida");
  84. System.out.println();
  85. System.out.println(" Months");
  86. System.out.println(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Avg ");
  87. System.out.println("***************************************************************************************************************************");
  88. 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]);
  89. System.out.println();
  90. 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);
  91. System.out.println();
  92. 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);
  93. System.out.println();
  94.  
  95.  
  96.  
  97. inFile.close();
  98.  
  99.  
  100.  
  101. }
  102. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Writing my program output to a text file.

 
0
  #2
Feb 3rd, 2009
  1. BufferedWriter writer = new BufferedWriter(new FileWriter("fileName"));
  2.  
  3. writer.write("write here");
  4. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC