#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    [Help] Calling methods, etc...


    I have a .dat file that contains 10 numbers

    apple.dat
    Code:
    99
    88
    77
    66
    55
    44
    33
    22
    11
    00
    I need to write a program that will do these three things:

    in one method:
    - Find the largest value in the apple.dat file.
    in another separate method:
    - Find the smallest value in the apple.dat file.
    in the last separate method:
    - Find the average of the values in the apple.dat file.

    Then in the MAIN method, call those submethods and print out their result.

    How would one go about doing this?



    I have this so far, but i think I'm going down the wrong track.
    Code:
    import java.util.Scanner;
    import java.io.*;
    public class Practice {
    
    
    public static void main(String[] args) {
    	
    	int menuInput;
    	Scanner inFile = new Scanner(new FileReader("apple.dat"));
    	Scanner inData = new Scanner(System.in);
    	int maxValue = max(inFile.nextInt());
    	int minValue;
    	int avgValue;
    	
    	System.out.println("for MAX value type 1");
    	System.out.println("for MIN value type 2");
    	System.out.println("for AVG value type 3");
    	System.out.println("---------------------");
    	System.out.println("Enter value:");
    	
    		menuInput = inData.nextInt();
    		
    	if (menuInput == 1){
    		System.out.println(maxValue);
    	} else if (menuInput == 2){
    		System.out.println(minValue);
    	} else if (menuInput == 3){
    		System.out.println(avgValue);
    	} else {
    		System.out.println("Incorrect input...Exiting...");
    	}
    }
    
    
    
    
    
    
    
    public static int max(int num) throws IOException {
    
        final int MAX = 10;
        //Scanner inFile = new Scanner(new FileReader("apple.dat"));
        int[]  numbers;
        numbers = new int[MAX];
    
        
        int  index;
        int value;
        for (index = 0; index < numbers.length; index++)
        {
     		numbers[index] = inFile.nextInt();
        }
        
        for (index = MAX - 1; index >= 0; index--)
        
       		//System.out.println(numbers[index]);
       		
       		return (numbers[index]);
       		
        inFile.close();
        
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What happens when the code is compiled and executed?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    It prints out the

    largest value
    smallest value
    average value

    which are called from the separate methods
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Is what you posted the desired output from the program?
    If not, please explain what you want the program to output and show an example.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    method1:

    return maxValue (max value of the list in this case being "99")

    method2:

    return minValue (min value of the list in this case being "00")

    method3:

    return avgValue (avg value of the list which would be all the numbers added together (495) divided by the amount of numbers (10) "49.5")

    main method:
    in quotes is what the program should print out
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you explain what problems you are having?
    Can you post the output from the program and add some comments saying what is wrong is the output and show what the out should be.

    Execute the program,
    copy the full contents of the console window
    paste it here.

IMN logo majestic logo threadwatch logo seochat tools logo