简体   繁体   中英

Dividing numbers between two different arrays in Java

This is my first post. Thank you in advance for your understanding and help on this topic. I am trying to divide two numbers between two different arrays. My data is being pulled from a.txt file. I'm given the name, gallons, and miles. I'm trying to calculate MPG. So I would have to divide gallons[0] by miles[0], gallons[1] by miles[1],etc.... and display this in the list in the cmd window. Below is the code that I have so far. Please note that the pseudo'd out method at the bottom was what I was trying before I tried the method above it. I apologize in advance if this has been addressed/answered in a previous thread.

import java.util.Scanner;
import java.io.*;
public class mpgHOmework2{
    public static void main(String[]args) throws IOException{

    File fn = new File("mpg.txt");
    Scanner inputFile = new Scanner(fn);

    Scanner keyboard = new Scanner(System.in);

    String[] names = new String[10];
    int[] miles = new int[10];
    int[] gallons = new int[10];
    double mpg;

    fillArray(inputFile,names,miles,gallons);

    displayArray(names,miles,gallons,mpg(miles,gallons));

    }//end main method
//==================================================================
    public static void fillArray(Scanner input,String[] arrIn, int[] arr1In, int[] arr2In){

        for(int indx = 0; indx < arrIn.length; indx++){
            arrIn[indx] = input.nextLine();
            if(arrIn[indx].length() == 0)arrIn[indx] = input.nextLine();
            arr1In[indx] = input.nextInt();
            arr2In[indx] = input.nextInt();}


    }//end fillArray method
//==================================================================
    public static void displayArray(String[] arrIn,int[] arr1In, int[]arr2In, double mpg){

        System.out.printf("%-18s   %5s    %7s   %3s\n","Names","Miles","Gallons","MPG");
        System.out.printf("%-18s   %5s    %7s   %3s\n","==================","=====","=======","===");
        for(int indx = 0; indx < arrIn.length; indx++)
            System.out.printf("%-18s   %5d   %7d  %.2f\n",arrIn[indx],arr1In[indx],arr2In[indx],mpg);

    }//end displayArray method
//==================================================================
    public static double mpg(Scanner input,String[] arrIn, int[] arr1In, int[] arr2In){

        for(int indx = 0; indx < arrIn.length; indx++){
            arrIn[indx] = input.nextLine();
            if(arrIn[indx].length() == 0)arrIn[indx] = input.nextLine();
            arr1In[indx] = input.nextInt();
            arr2In[indx] = input.nextInt();
        return = arr1In[indx]/arr2In[indx];}
}enter code here
/*  public static double mpg(int[] arrOneIn, int[] arrTwoIn){

        double retValue = 0.0;
            int a = 0;
            int b = 0;
            for (int i = 0; i < arrOneIn.length-1; i++){
                return [i] = arrOneIn[i] / arrTwoIn[i];
                }

            return a/b;
    }//end mpg method*/
}//end class

This is the mpg.txt file. Sorry I just signed up 10 min ago and don't know how to navigate the site yet.

John Murphy
1200  60
Katherine Cleary
1500 63
Isaac Fajerman
2600 87
Marsha Smiith
3500 100
Jason Meier
4100 196
Inna Johnson
750 34
Walter Philips
6900 265
Tracey Cannon
5024 187
Sheryl Rothman
6800 323
Jeff Gottlieb
450 18

Thanks for your time.

-Rob

Here is your main problem. Your mpg method should look like this. You need to return all the averages in an array so the return values need to be an array.

And you need to cast one of the int[] arrays to double so you don't drop the fractions in the division process.


    public static double[] mpg(int[] arrOneIn, int[] arrTwoIn) {

        double[] retValues = new double[arrOneIn.length];
        for (int i = 0; i < arrOneIn.length; i++) {
            retValues[i] = (double) arrOneIn[i] / arrTwoIn[i];
        }
        return retValues;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM