Results 1 to 5 of 5
Thread: Errors?
- 12-28-2009, 11:49 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
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 05:48 AM. Reason: Code tags
- 12-29-2009, 01:02 AM #2
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:
Try something like this to initialize your data arrays:Java Code:while (in3.hasNext()) { while (in4.hasNext()) { for (int hum: dataArr3) { hum = in3.nextInt(); for (double temper: dataArr4) { temper = in4.nextDouble();
Java Code:int count = 0; while(in3.hasNext()) { dataArr3[count++] = in3.nextInt(); } count = 0; while(in4.hasNext()) { dataArr4[count++] = in4.nextDouble(); }
- 12-29-2009, 01:57 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, please use code tags when you posting code segments next time. Unformated codes are hard to read.
- 12-29-2009, 04:01 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
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.
- 12-30-2009, 12:14 AM #5
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:
The array dataArr4 has been allocated and its elements are all assigned the default value of 0.0 by the jvm.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];
This later construct
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.Java Code:for (int hum: dataArr3) { hum = in3.nextInt();
Similar Threads
-
errors
By ravikumar in forum Threads and SynchronizationReplies: 3Last Post: 09-29-2009, 04:15 AM -
getting errors
By ravikumar in forum Threads and SynchronizationReplies: 3Last Post: 08-23-2009, 02:50 PM -
Errors.
By rocky in forum New To JavaReplies: 4Last Post: 04-09-2009, 08:05 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks