Hey guys. I am trying to write a programme (not using scanner class) which prints the smallest number in an external text file called README.txt. Anyway I included some explanation of what I am doing so hopefully it's clear. What this programme prints is 0.0, which is not even a part of my set of doubles.

README.txt consists of the following doubles:
{8.5 1.6 7.4 12.9 8.04 5.13 4.17 4.05 7.86}

Thank you :)

public class minimum
   public static void main (String [] args)
       double Minimum;
       double [] data = createArray();
       int a = 0;
       int b = 0;
   Minimum = minimum (data,a,b);
   System.out.println (Minimum);
   public static double[] createArray() //a method to  create arrays
   int b = 0;
   double [] data = new double[100];
   TextFile inFile   = new TextFile (true, "README.txt");
   for (int a = 0; a < 100 && !inFile.eof(); a++)
   data [a] = inFile.readDouble ();
   b++;                         // b is the counter
   return data;
   public static double minimum (double [ ] data, int a, int  b)
   double result;
   a = 0; // I want a to start at 0. 
   if (data [a] > data [b])  // so my reasoning is that           if array [a] is smaller than
                             //b, the result becomes [a] and the loop continues with the
   result = data [b];        // new value for a, which has incremented by one         

   if (data [a] < data [b])
   result = data [a];         // and vice versa, but b decreases by one as it defined 
   b = b-1;            // (i think) as the number of doubles in the file.
   return result;
  } while (a != b);     // I want it to stop as soon as this occurs and return the
                        // smallest value in the array

Edited by herstein: n/a

5 Years
Discussion Span
Last Post by JamesCherrill

Where do you call createArray ?

Thanks for the reply! Think I may have fixed it, is that better?
However, the programme now prints "error: "" is not a legal double". I've had this problem before but haven't really been able to fix it cos for me it's quite clear that I want it to stop once it reaches the end of the file

Edited by herstein: n/a


I guess that's from the inFile.readDouble ()? The TextFile class doesn't seem to be part of the standard Java SE API, so I really can't comment on it.

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.