-
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.
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();
}
}
}
-
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:
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:
Code:
int count = 0;
while(in3.hasNext()) {
dataArr3[count++] = in3.nextInt();
}
count = 0;
while(in4.hasNext()) {
dataArr4[count++] = in4.nextDouble();
}
-
@OP, please use code tags when you posting code segments next time. Unformated codes are hard to read.
-
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.
-
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:
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
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.