Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting and reading files, using loops, and analyzing data
    primarykey
    data
    text
    <p>Im having a little trouble finishing my program. Here is what Ive got and the instructions. Create a file that holds 500 random numbers ranging from 0 - 1000, using a for loop to write the numbers into the file. Then 1. Find the smallest value 2. Find the largest value 3. Find the average and 4. determine how many time consecutive numbers came up. Im having issues with creating the random number file and step 4. Thanks in advance!</p> <pre><code> public static void main(String[] args) throws FileNotFoundException { PrintWriter prw = new PrintWriter("results.txt"); File numfile = new File("randomdata.txt"); Scanner infile = new Scanner(numfile); int num, largest, smallest, sum = 0, count = 0; int programnumber = 6; header (prw, programnumber ); double average = 0; Random gen = new Random(); System.out.println("From gen: "); for (int i = 0; i &lt; 1000; i++){ System.out.print(gen.nextInt(500) + ""); num = gen.nextInt(500); System.out.println(num); sum = sum + num; count++; average = (double) sum / count; } while (infile.hasNext()){ num = infile.nextInt(); largest = infile.nextInt(); smallest = infile.nextInt(); System.out.println(num); if (num &lt; largest){ largest = num; } if (num &gt; smallest){ smallest = num; } System.out.println("The largest is: " + largest); prw.println("The largest is: " + largest); System.out.println("The smallest is: " + smallest); prw.println("The smallest is: " + smallest); System.out.println("The average is: " + average); prw.println("The average is: " + average); } prw.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload