1 2 Previous Next 28 Replies Latest reply: Jan 19, 2009 11:50 PM by 796440 RSS

    help with input files and loops

    807588
      Hi, is there a way I can get the data from the two files, and put it through the formula and display it?

      thanks

      The two text files.

      KeyWestTemp =
      70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0 78.1

      KeyWestHumid =
      69 67 66 64 66 69 67 67 70 69 69 70 68
      /**
       * Write a description of class HeatIndex here.
       * 
       * @author (your name) 
       * @version (a version number or a date)
       */
      import java.util.Scanner;
      import java.io.File;
      import java.io.IOException;
      public class HeatIndex
      {
        public static void main(String[] args) throws IOException
         {  
            String title = "Heat Index: "; 
            String city1 = "Key West, Florida";
            System.out.printf("%35s%1s\n ", title, city1); 
            System.out.println("");
            System.out.printf("%40s\n ", "Months");
            String[] months = { "  Jan ", " Feb ", "  Mar ", "  Apr ", "  May ", "  Jun ", "  Jul ", "  Aug ", "  Sep ", "  Oct ", "  Nov ", "  Dec ", "  Avg "};
             for ( String name : months ) 
            { 
              System.out.print(name); 
             }
            System.out.println(""); 
            System.out.println("*******************************************************************************");
            
            
            String token = "";
            File fileName = new File("KeyWestTemp.txt");
            Scanner inFile = new Scanner(fileName);
            while (inFile.hasNext())
            {
               token = inFile.next( );
               System.out.printf("%6s",token);
            }
            inFile.close();   
            
            System.out.println(""); 
            System.out.println("");
            
            String token1 = "";
            File fileName1 = new File("KeyWestHumid.txt");
            Scanner inFile1 = new Scanner(fileName1);
            while (inFile1.hasNext())
            {
               token1 = inFile1.next( );
               
      
               System.out.printf("%6s", token1);
            }
            inFile.close(); 
            
            System.out.println("");
            
            System.out.println("");
            
            double temp = 90;
            double hum = 70;
            
             double heatindex = -42.379 + 2.04901523 * temp + 10.14333127 * hum-0.22475541*temp*hum-0.00683783*Math.pow(temp,2)-.05481717*Math.pow(hum,2)+.00122874*Math.pow(temp,2)*hum+.00085282*temp*Math.pow(hum,2)-.00000199*Math.pow(temp*hum,2);
             heatindex =Math.round(100*heatindex)/100; 
            
       
             
             System.out.println(heatindex);
       
           
      
      
      
            
          }
      }
        • 1. Re: help with input files and loops
          807588
          bmxriderss wrote:
          Hi, is there a way I can get the data from the two files, and put it through the formula and display it?
          Yes. Do you have a specific question?
          • 2. Re: help with input files and loops
            807588
            how would i be able too do it?

            thanks
            • 3. Re: help with input files and loops
              807588
              By writing correct code.

              (question is still not specific enough)
              • 4. Re: help with input files and loops
                807588
                What flounder is trying to ask is: How do you want the code to be displayed? Graphs? Tables? In a little text area? In a table? that sort of thing. More details are needed before we can help
                • 5. Re: help with input files and loops
                  807588
                  HDL_CinC_Dragon wrote:
                  What flounder is trying to ask is: How do you want the code to be displayed? Graphs? Tables? In a little text area? In a table? that sort of thing. More details are needed before we can help
                  Don't put words in my mouth. There is a lot of things the OP needs to work on before they even consider presentation. It is these things that I want them to ask a coherent question about.
                  • 6. Re: help with input files and loops
                    807588
                    I would use Apache Commons Configuration for this see...

                    [Commons Configuration List Handling|http://commons.apache.org/configuration/howto_basicfeatures.html#List_handling]

                    And set your delimiter to be a ' '.

                    This will return you a List of the values in the file.
                    • 7. Re: help with input files and loops
                      807588
                      @flounder
                      Noted. I wont put words in your mouth anymore.

                      There is advantage to aiming higher than your current level of knowledge allows for though. In order to get there you are forced to learn the stepping stones to get there. I do it all the time. I pick a task that I know I dont have the knowledge to complete so that im forced to learn everything between where I am and where I want to end up. Though usually I do a lot of internet searching before posting a question on a forum.
                      • 8. Re: help with input files and loops
                        807588
                        i just wanted it to be displayed with System.out.println(""). Just how, with loops or if then?
                        • 9. Re: help with input files and loops
                          807588
                          Geez, you still have not put much thought into your question. However I will make an attempt to help.

                          Currently you have two loops, each one reads a values from one file and display it. As soon as you read the next value, the previous value is lost. Do you want to keep those values somewhere perhaps. Or maybe just use one loop that reads a value form file1 and one value from file2 and then does something with them.
                          • 10. Re: help with input files and loops
                            807588
                            Hi i think this does what you want...
                            Need a bit of tidying up though.
                            /**
                             * Write a description of class HeatIndex here.
                             * 
                             * @author (your name) 
                             * @version (a version number or a date)
                             */
                            import java.util.ArrayList;
                            import java.util.Iterator;
                            import java.util.List;
                            import java.util.Scanner;
                            import java.io.File;
                            import java.io.IOException;
                            
                            public class HeatIndex {
                                 public static void main(String[] args) throws IOException {
                                      String title = "Heat Index: ";
                                      String city1 = "Key West, Florida";
                                      System.out.printf("%35s%1s\n ", title, city1);
                                      System.out.println("");
                                      System.out.printf("%40s\n ", "Months");
                                      String[] months = { "  Jan ", " Feb ", "  Mar ", "  Apr ", "  May ",
                                                "  Jun ", "  Jul ", "  Aug ", "  Sep ", "  Oct ", "  Nov ",
                                                "  Dec ", "  Avg " };
                                      for (String name : months) {
                                           System.out.print(name);
                                      }
                                      System.out.println("");
                                      System.out
                                                .println("*******************************************************************************");
                            
                                      String token = "";
                                      File fileName = new File("KeyWestTemp.txt");
                                      //   System.out.println(fileName.getAbsolutePath());
                                      Scanner inFile = new Scanner(fileName);
                            
                                      List keyWestTemp = new ArrayList();
                                      while (inFile.hasNext()) {
                                           token = inFile.next();
                                           keyWestTemp.add(token);
                                           System.out.printf("%6s", token);
                                      }
                                      inFile.close();
                            
                                      System.out.println("");
                                      System.out.println("");
                            
                                      String token1 = "";
                                      File fileName1 = new File("KeyWestHumid.txt");
                            
                                      Scanner inFile1 = new Scanner(fileName1);
                                      List keyWestHumid = new ArrayList();
                                      while (inFile1.hasNext()) {
                                           token1 = inFile1.next();
                            
                                           keyWestHumid.add(token1);
                                           System.out.printf("%6s", token1);
                                      }
                                      inFile.close();
                            
                                      System.out.println("");
                            
                                      System.out.println("");
                            
                                      //check we have the same number of data records.
                                      if (keyWestHumid.size() != keyWestTemp.size()) {
                                           System.out.println("Numer of records is not equal");
                                      }
                            
                                      for (int i = 0; i < keyWestTemp.size(); i++) {
                            
                                           double temp = Double.valueOf((String) keyWestTemp.get(i));
                                           double hum = Double.valueOf((String) keyWestHumid.get(i));
                                           double heatindex = -42.379 + 2.04901523 * temp + 10.14333127 * hum
                                                     - 0.22475541 * temp * hum - 0.00683783 * Math.pow(temp, 2)
                                                     - .05481717 * Math.pow(hum, 2) + .00122874
                                                     * Math.pow(temp, 2) * hum + .00085282 * temp
                                                     * Math.pow(hum, 2) - .00000199 * Math.pow(temp * hum, 2);
                                           heatindex = Math.round(100 * heatindex) / 100;
                                           System.out.printf("%6s", heatindex);
                                      }
                            
                                 }
                            }
                            • 11. Re: help with input files and loops
                              807588
                              Jeebus! Why are there so many attention whoring buttholes that are prepared to just post code rather than make the OP think for themselves?
                              • 12. Re: help with input files and loops
                                807588
                                Some people learn better from studying source code and tweaking it rather than condescending comments from people who already know what theyre talking about.
                                • 13. Re: help with input files and loops
                                  807588
                                  Just being helpful - rather than consistently having a go at the OP's question asking style. It is only 6 lines of code they - were 95% done anyway.
                                  • 14. Re: help with input files and loops
                                    807588
                                    Then let them do the other 5% as well.
                                    1 2 Previous Next