0

Can somebody help me understand why I get an error message that says possible loss of precision on this program? I don't understand what could be causing this error message. When I get this error message, it highlightes the line of code that says: "theTemps[index] = inFile.next();". Can somebody enlighten me as to why I get this error message? Thanks

/**
 * This program calculates the monthly Heat Index for a specific city. 
 * 
 * @author John D. Barry 
 * @date  January 30, 2009
 */
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
class HeatIndex

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

           double index = 0.0;
           double [] theTemps = new double[14];
           File temps = new File("KeyWestTemp.txt");
           Scanner inFile = new Scanner(temps);
           
           while (inFile.hasNext())
           {
            
                theTemps[index] = inFile.next();

           }

           inFile.close();

           


            

            

            
            

            


            
            

       

    }
}
2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Ezzaral
0

Ok, there are actually two issues on that line. Your "index" variable is declared as double but used as an integer index to your array. It should be declared int. Secondly, inFile.next() returns a String. You cannot directly store that into an array of double.

0

ok. How do I make inFile.next() return a double then, and not a String

0

Ok. Now I have changed it to nextDouble, however, it is still not reading in the correct doubles that I have stored.

/**
 * This program calculates the monthly Heat Index for a specific city. 
 * 
 * @author John D. Barry 
 * @date  January 30, 2009
 */
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
class HeatIndex

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

           int index = 0;
           double [] theTemps = new double[14];
           File temps = new File("KeyWestTemp.txt");
           Scanner inFile = new Scanner(temps);
           
           while (inFile.hasNextDouble())
           {
            
                theTemps[index] = inFile.nextDouble();

           }
           System.out.println(theTemps);

           inFile.close();

           


            

            

            
            

            


            
            

       

    }
}
0

I would guess that only the first element of your array has a value and that it is the last double from your file... ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.