简体   繁体   中英

Seperate method not returning an array value to main

I am stuck on something and if I figure it out I can finish the rest of the assignment for school. I am having trouble returning array values back to the main. I use the readData() method to read double values from a text file into my array double [] miles . If I use the for loop to print inside the readData() method it prints the values, but if I use it in the main() under the method call, it doesn't print off the values. What am I doing wrong here? Program can compile and run but isn't complete. All questions, comments, concerns and advice are welcome. Please help

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class CollinDunn_1_08 {   
   static Toolkit tools = new Toolkit();
   public static void main (String [] args) throws IOException {    

      //I/O String references
      final String INPUT_FILE = "CollinDunn_1_08_Input.txt" ;
      final String OUTPUT_FILE = "CollinDunn_1_08_Output.txt"; 

      //Variable and Array Declarations
      int numValues = 0;
      double [] miles = new double [0]; 
      double [] payMe = new double [0];

      // These are string references for methods
      String genInfo = "";    
      String headInfo = "";
      String outInfo = "";

      //Access Input and Output
      FileInputStream inputDataFile = new FileInputStream(INPUT_FILE);
      Scanner inputFile  = new Scanner(inputDataFile);    
      FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
      PrintWriter outputFile = new PrintWriter(outputDataFile);

      // Initiate program
      System.out.println("Initiating program.");

      // progInfo method, this displays
      // the genral info about the program
      progInfo(outputFile, genInfo);  

      // readData method, this reads the
      // mileage values from the file and 
      // stores in in the miles array.
       readData(miles, inputFile, numValues);

       for(int i = 0; i < numValues; i++) {
         System.out.print(miles[i] + "\n");
      }

      //Calculate reimbursements

      printHeading(outputFile, headInfo);

      payCalc(miles, payMe, numValues);


   } // End main

   //This method reads the mile data from a file and stores it
   public static double [] readData(double [] miles, Scanner inputFile, int numValues) {
      numValues = inputFile.nextInt();
      miles = new double [numValues];
      for(int i = 0; i < numValues; i++) {
         miles[i] = inputFile.nextDouble();
      }        
      return miles;
   } // End readData

   // A method used for a program description
   public static void progInfo(PrintWriter outputFile, String genInfo) {
      genInfo = "This program reads miles from a input file,"
                       + "\nstores the values in an array, calculates the"
                       + "\nreimbursement and then stores them in a"
                       + "\nparalell array.";
      outputFile.println(genInfo);
      System.out.println(genInfo);
      return;
   } // End progInfo

   // A method for printing out the heading
   public static void printHeading(PrintWriter outputFile, String headInfo) {
      headInfo = "Mileage" + "\t\t" + "Rimbursements";
      outputFile.println(headInfo);
      System.out.println(headInfo);
   } // End printHeading

   // A method for outputing the results of the calcualtions
   public static void printTable(PrintWriter outputFile, String outInfo) {
      outInfo = "";

   } // End printTable

   // This method calculates the reimbursement amount
   // and stores it in its appropriate array.
   public static double [] payCalc(double [] miles, double [] payMe, int        numValues) {
      for (int i = 0; i < numValues; i++) {
         if (miles[i] <= 0) {
            payMe[i] = 0.0;
         } else if (miles[i] <= 400) {
            payMe[i] = miles[i] * 0.18;
         } else if (miles[i] >= 400 && miles[i] < 900) {
            payMe[i] = 65.00 + 0.15 * (miles[i] - 400);
         } else if (miles[i] >= 900 && miles[i] < 1300) {
            payMe[i] = 115.00 + 0.12 * (miles[i] - 900);
         } else if (miles[i] >= 1300 && miles[i] < 1900) {
            payMe[i] = 140.00 + 0.10 * (miles[i] - 1300);
         } else if (miles[i] >= 1900 && miles[i] < 2600) {
            payMe[i] = 165.00 + 0.08 * (miles[i] - 1900);
         } else if (miles[i] >= 2600) {
            payMe[i] = 195.00 + 0.06 * (miles[i] - 2600);
         } // End if/else      
      } // End for loop
   return payMe;
   }

} //End class 

Re-assign miles of main() method as follows

miles = readData(miles, inputFile, numValues);

Inside readData() ,

when you wrote miles = new double [numValues]; then mile local variable of readData() are no longer referencing the local variable of main() named mile .

So you need to reassign of your local variable mile of main() method by returing value of readData method.

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