Results 1 to 5 of 5

Thread: Errors?

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

    Default Errors?

    First off, I am getting the following errors:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at heatpracticing2.main(heatpracticing2.java:131)

    I don't know what these mean, and I don't know how to fix them, and google is no help. The assignment is that I have to read temperatures and humidities from 2 separate files and calculate heat index. The heat index formula is really long but I checked it and made sure that I typed it correctly but my heat index numbers are very slightly off (within 5). I'm thinking the errors are related to the inaccuracy.

    The humidity file has the following values:
    69 67 66 64 66 69 67 67 70 69 69 70

    The temperature file has the following values:
    70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0

    The issue is towards the bottom with the heat index calculations. My output is the following (with the errors):

    Heat Index: Key West, Florida

    Months
    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Avg
    ************************************************** **************
    Temp: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.0
    Hum: 69 67 66 64 66 69 67 67 70 69 69 70 60
    HI: 74.1 74.3 75.8 78.7 83.9 88.9 91.2 91.0 88.9 83.1 78.0 74.7 81.9Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at heatpracticing2.main(heatpracticing2.java:131)




    Here is my code and I thank everyone.



    Java Code:
    /**
     * Jamison Hyman
     *
     *
     * 12/25/09 (Christmas)
     * This is practicing how to read data into an array from a file.
     */
    
    import java.util.Scanner;
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    public class heatpracticing2 {
    
    public static void main(String[] args) throws IOException
    {
    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 ************************************************************************************************************************
    System.out.print("Temp:");    
    Scanner in = new Scanner(new File("KeyWestTemp.txt"));
    
    int count = 0;
    
    while(in.hasNext()) {
        in.nextDouble();
        count++;
    }
    in.close();
    double[] dataArr = new double[count];
    count = 0;
    in = new Scanner(new File("KeyWestTemp.txt"));
    double sum = 0;
    while(in.hasNext()) {
        for(double temp: dataArr)
        {
        temp = in.nextDouble();
        sum += temp/12;
        System.out.printf("%-5.4s",temp);
        }   
        System.out.printf("%.4s",sum);    
           }
           System.out.println();
    in.close();      
    
    // HUMIDITY *************************************************************************
    
    System.out.print("Hum: ");    
    Scanner in2 = new Scanner(new File("KeyWestHumid.txt"));
    
    int count2 = 0;
    
    while(in2.hasNext()) {
        in2.nextInt();
        count2++;
    }
    in2.close();
    int [] dataArr2 = new int [count2];
    count2 = 0;
    in2 = new Scanner(new File("KeyWestHumid.txt"));
    int sum2 = 0;
    while(in2.hasNext()) {
        for(int humid: dataArr2)
        {
        humid = in2.nextInt();
        sum2 += humid/12;
        System.out.printf("%-5.4s",humid);
        
        }   
        System.out.printf("%.4s",sum2);    
           }
    in2.close(); 
    System.out.println();
    
    // HEAT INDEX CALCULATION ***************************************************************************
    
    //int tokenr = 0; Don't use these?
    //double tokent = 0; ^^
    
    System.out.print("HI:  ");
    Scanner in3 = new Scanner (new File("KeyWestHumid.txt"));
    
    int count3 = 0;
    
    while(in3.hasNext()) {
        in3.nextInt();
        count3++;
    }
    in3.close();
    int [] dataArr3 = new int [count3];
    
    Scanner in4 = new Scanner (new File("KeyWestTemp.txt"));
    int count4 = 0;
    while(in4.hasNext()) {
        in4.nextDouble();
        count4++;
    }
    in4.close();
    
    double [] dataArr4 = new double [count4];
    
    in3 = new Scanner(new File("KeyWestHumid.txt"));
    in4 = new Scanner(new File("KeyWestTemp.txt"));
    
    double sum3 = 0;
    double sum4 = 0;
    double total = 0;
    double avg = 0;
    
    while (in3.hasNext())       
    {
        while (in4.hasNext())       
            {  
                for (int hum: dataArr3)
                {
                    hum = in3.nextInt();
                    
                     for (double temper: dataArr4)
                    {
                temper = in4.nextDouble();        
    [B][I][U]            total = (-42.379 + (2.04901523*temper) + (10.14333127*hum) - (0.22475541*temper*hum)
             - (6.83783 * .001 * temper*temper) - (5.481717*.01*hum*hum) +
            (1.22874 * .001*temper * temper * hum) + (8.5282 * .0001*temper*hum*hum) - 
            (1.99*.000001*temper*temper*hum*hum) ); // FORMULA[/U][/I][/B]
            
                avg += (total/12);
                System.out.printf("%-5.4s",total);
                }
                    System.out.printf("%.4s",avg);
                }
    }
        
                in3.close();
                in4.close();        
    }
    
        }
    }
    Last edited by Jamison5213; 12-29-2009 at 06:48 AM. Reason: Code tags

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    NoSuchElementException is thrown by both the nextInt and nextDouble methods in the Scanner api. Sounds like you've gotten your loop logic out of synch.
    This bit of code looks like trouble:
    Java Code:
    while (in3.hasNext()) 
    {
        while (in4.hasNext()) 
        { 
            for (int hum: dataArr3)
            {
                hum = in3.nextInt();
    
                for (double temper: dataArr4)
                {
                    temper = in4.nextDouble();
    Try something like this to initialize your data arrays:
    Java Code:
    int count = 0;
    while(in3.hasNext()) {
        dataArr3[count++] = in3.nextInt();
    }
    
    count = 0;
    while(in4.hasNext()) {
        dataArr4[count++] = in4.nextDouble();
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP, please use code tags when you posting code segments next time. Unformated codes are hard to read.

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

    Default

    I can't figure out how to do that without rearranging my variables hum and temper. They can't be the same value as dataArr3 and dataArr4.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    can't figure out how to do that without rearranging my variables hum and temper
    You have some changes to make to get what you've posted to work.
    Consider this small snippet from above:
    Java Code:
    Scanner in4 = new Scanner (new File("KeyWestTemp.txt"));
    int count4 = 0;
    while(in4.hasNext()) {
        in4.nextDouble();
        count4++;
    }
    in4.close();
    
    double [] dataArr4 = new double [count4];
    The array dataArr4 has been allocated and its elements are all assigned the default value of 0.0 by the jvm.
    This later construct
    Java Code:
                for (int hum: dataArr3)
                {
                    hum = in3.nextInt();
    looks like an attempt to assign the values read from the file to the newly–allocated array elements. But this for-each loop is for show only; you cannot alter any of the elements in the array.

Similar Threads

  1. errors
    By ravikumar in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-29-2009, 05:15 AM
  2. getting errors
    By ravikumar in forum Threads and Synchronization
    Replies: 3
    Last Post: 08-23-2009, 03:50 PM
  3. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 09:05 AM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 05:55 PM

Posting Permissions

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