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();

           


            

            

            
            

            


            
            

       

    }
}

Recommended Answers

All 6 Replies

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.

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

Glance through the methods on the Scanner class. I think you'll spot a couple that stand out.

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();

           


            

            

            
            

            


            
            

       

    }
}

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

Hint: trace the value of "index" through your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.