Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a text file with 10 integers, 1 2 3 4 5 6 7 8 9 10. They print out in order however I am trying to find the smallest, largest and average out of the integers in the text file.

What I have tried:

Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Exercise2txt {

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

		int[] numbers = new int[10];

		int largest = Integer.MIN_VALUE;
		int smallest = Integer.MAX_VALUE;
		double total = 0;
		double avg;
		int i = 0;

		
			Scanner inFile = new Scanner(new File("numbers.txt"));

			for (int y = 0; y < 10; y++) 
				System.out.println(inFile.nextInt());
			
			if (numbers[i] > largest) {
				largest = numbers[i];
			}

			if (numbers[i] < smallest) {
				smallest = numbers[i];
			}

			for (int x = 1; x < numbers.length; x++) {
				total += numbers[x];
			}

			avg = total / numbers.length;
		

		System.out.println("Largest number = " + largest);
		System.out.println("Smallest number = " + smallest);
		System.out.println("The average of all numbers = " + avg);

	}
}


This is the code i have at the moment
Posted
Updated 11-Oct-18 22:00pm
v2

Quote:
They print out in order

This is exactly what you do in your code.
Java
System.out.println(inFile.nextInt());


This line is reading from file and printing, but where is the magic that save the integer for future use ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Your code is almost complete, you've just to properly read the file. Try
Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Exercise2txt {

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

    int[] numbers = new int[10];

    int largest = Integer.MIN_VALUE;
    int smallest = Integer.MAX_VALUE;
    int count = 0;
    double sum = 0.0;
    double avg;
    Scanner inFile = new Scanner(new File("numbers.txt"));
    while ( inFile.hasNext())
    {
      int n = inFile.nextInt();
      if ( largest < n)
        largest = n; 
      if (smallest > n)
        smallest = n;
      sum += n;
      ++count;
    }
    avg = sum / count;

    System.out.println("Largest number = " + largest);
    System.out.println("Smallest number = " + smallest); 
    System.out.println("The average of all numbers = " + avg);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900