Results 1 to 4 of 4
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default Using methods and arrays after importing files

    The issue I am having is how to import data from 2 text files into arrays, then use the data from the array. I was able to import data from a txt file, and print the data but I didn't use an array. I should have done the array and then used the formula to apply to each value in the array. I can import the data and read the data and print the data but I can't set it to an array [all this defined as first problem]. I have used a "For-each loop" when I have entered numbers manually into my array. However, I've been told that when I'm importing data into an array I should use a method and call it but I do not know how to do that either even after having read about it [problem 2]. My output is fine, I have just have to re-arrange the code to fit the teacher's requirements. What would you suggest? The instructions are as follows and my code is underneath that.




    Instructions: Write a program to calculate the monthly Heat Index for a specific city.
    1. Create a new project called 6.03 Weather Data in the Mod06
    Assignments folder.
    2. Create a class called HeatIndex in the newly created folder.
    3. Be sure you have downloaded the KeyWestTemp.txt and the KeyWestHumid.txt files to the
    project folder.
    4. Break the program into functional units so that you can use separate for loops and arrays to
    handle specific tasks.
    5. Your program should read in the data from the two text files.
    6. Be sure to use the for-each loop when it is appropriate.
    7. Calculate the monthly Heat Index for Key West, Florida.
    8. Notice from the table shown in the 6.03 Virtual Lecture Notes that if the temperature and
    humidity are below certain levels, the actual temperature and the apparent temperature (i.e.
    the Heat Index) are identical. For example, in the expected output shown below, notice that
    the Heat Index is only calculated for the months of May October.
    9. Use the Formatting Grid you downloaded in the last lesson to plan your user-friendly
    output.
    10. Print the results in a user-friendly format (i.e. use printf()) with columns for the Month,
    Temperature, Relative Humidity, and the Heat Index.
    11. Write the Heat Index data to a file. Include this file when you submit your program.
    12. Writing the lengthy Heat Index formula can be very error prone, so watch out for the Order
    of Operations.



    The code is as follows


    /*
    * Jamison Hyman
    * 12/19/09
    * This program generates a random password.
    */
    import java.util.Scanner;
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    public class HeatIndex {

    public static void main(String[] args) throws IOException

    {

    // File in1 = new File("KeyWestHumid.txt");
    // File in2 = new File("KeyWestTemp.txt");
    String location = ("Heat Index: Key West, Florida");
    String namemonths = ("Months");
    String months [] = {"Jan ","Feb ","Mar ","Apr " ,"May ","Jun ","Jul ","Aug ","Sep ",
    "Oct ","Nov ","Dec ","Avg"};


    System.out.printf("%46s\n",location);
    System.out.println();
    System.out.printf("%34s\n", namemonths);
    System.out.println("\t " + months[0] + months[1] + months[2] + months[3] + months[4] + months[5] + months[6]
    + months[7] + months[8] + months[9] + months[10] + months[11] + months[12]);
    System.out.println("\t" + " ************************************************** *************");
    //temperature
    double token2 = 0.0;
    double sum2 = 0.0;
    System.out.print("Temp:");
    Scanner inFile2 = new Scanner (new File("KeyWestTemp.txt"));


    while (inFile2.hasNext())
    {
    token2 = inFile2.nextDouble();
    System.out.printf("%-5.4s",token2);
    sum2 += token2;
    }
    System.out.printf("%.4s",sum2/12);
    inFile2.close();

    System.out.println();


    int token = 0;
    int sum = 0;

    System.out.print("H: ");
    Scanner inFile = new Scanner (new File("KeyWestHumid.txt"));
    while (inFile.hasNext())
    {
    token = inFile.nextInt();
    System.out.printf("%-5s",token);
    sum += token;
    }
    System.out.println(sum/12);
    inFile.close();






    double tokenr = 0;
    double sum3 = 0;
    double tokent = 0;
    double sum4 = 0;
    System.out.print("HI: ");
    Scanner inFile3 = new Scanner (new File("KeyWestHumid.txt"));
    Scanner inFile4 = new Scanner (new File("KeyWestTemp.txt"));
    while (inFile3.hasNext())
    {

    while (inFile4.hasNext())
    {
    tokenr = inFile3.nextInt();
    tokent = inFile4.nextDouble();
    System.out.printf("%-5.4s",(-42.379 + (2.04901523*tokenr) + (10.14333127*tokent) - (0.22475541*tokent*tokenr)
    - (6.83783 * .001 * tokenr*tokenr) - (5.481717*.01*tokent*tokent) +
    (1.22874 * .001*tokenr * tokenr * tokent) + (8.5282 * .0001*tokenr*tokent*tokent) -
    (1.99*.000001*tokenr*tokenr*tokent*tokent) ));
    sum3 += (-42.379 + (2.04901523*tokenr) + (10.14333127*tokent) - (0.22475541*tokent*tokenr)
    - (6.83783 * .001 * tokenr*tokenr) - (5.481717*.01*tokent*tokent) +
    (1.22874 * .001*tokenr * tokenr * tokent) + (8.5282 * .0001*tokenr*tokent*tokent) -
    (1.99*.000001*tokenr*tokenr*tokent*tokent));

    }
    }
    System.out.printf("%.4s", sum3/12);
    inFile3.close();
    inFile4.close();

    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The issue I am having is how to import data from 2 text files into arrays, then use the data from the array.
    If I understand you correctly you want to read the data into arrays instead of processing the data one line at a time. To do this you have to read the data files twice: the first time to figure out how big they are (how many lines they have). Using this number you can create the arrays. Then you read the file a second time to populate the array, ie put the numbers from the file into the array.

    Java Code:
    Scanner in = new Scanner(new File(/*whatever*/));
    int count = 0;
    while(in.hasNext()) {
        in.nextDouble();
        count++;
    }
    in.close();
    double[] dataArr = new double[count];
    count = 0;
    in = new Scanner(new File(/*whatever*/));
    while(in.hasNext()) {
        dataArr[count] = in.nextDouble();
    }
    in.close();
    // now dataArr has all the numbers from the file and you
    // can loops and things to print or calculate values from them
    If you have studied them, a List is a much better option as they grow as required.

  3. #3
    gtamob69 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Jamison I sent you an important PM about java

  4. #4
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    I don't see it gtamob...and thank you very much pbrockway2. It was a lot of help.

Similar Threads

  1. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 10:43 AM
  2. importing class files
    By bizso in forum New To Java
    Replies: 6
    Last Post: 03-28-2009, 03:11 PM
  3. importing .java files into eclipse
    By nickl2002 in forum Eclipse
    Replies: 1
    Last Post: 01-19-2009, 12:06 AM
  4. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 08:37 PM
  5. Exporting/Importing JAR files
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •