I have the program here. This program uses the double_it method to double the va
ID: 3638305 • Letter: I
Question
I have the program here. This program uses the double_it method to double the values individually and stores them into a file. Could you guys please help me, how to double the whole array (not individual values) and store them into another file!!!!import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class DoubleGrades {
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file containing grades to be doubled.");
File file = new File(keyboard.nextLine()); // get the input file
Scanner input = new Scanner(file);
double [] grades = new double[100]; //100 grades can be accepted
int i = 0;//counter
while(input.hasNext()){
grades[i++] = doubleIt(Double.parseDouble(input.nextLine())); //double and store values
}
input.close(); //close the input file
System.out.println("Enter the name of the file to output doubled grades to.");
File outFile = new File(keyboard.nextLine());
PrintWriter printer = new PrintWriter(outFile);
for(int n = 0; n < i; n++){
printer.print(grades[n] + " ");
}
printer.close();
}
private static double doubleIt(double number){
return number * 2;
}
}
Explanation / Answer
Hi, if I understood correctly, you want the doubleIt method modified so that instead of passing individual values to it for doubling, you give it the whole array at once and get it all doubled?
Here's my version of your code, modified to do that. If I understood it wrong or there's something missing/unclear, send me a PM and we can look at it again. I highlighted the parts of the code that I modified in your class. Don't expect to just copy-paste it and have it work on the spot. Read through it and test it a few times to make sure you understand what goes on.
Note: I'm assuming the file reading and writing part works fine (it looks fine)
I hope this is helpful, please remember to rate :)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class DoubleGrades {
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file containing grades to be doubled.");
File file = new File(keyboard.nextLine()); // get the input file
Scanner input = new Scanner(file);
double [] grades = new double[100]; //100 grades can be accepted
double[] gradesDoubled = new double[100]; // up to 100 grades after they've been doubled
int i = 0;//counter
while(input.hasNext()){
// store the values as they are
grades[i++] = Double.parseDouble(input.nextLine()); // store values
}
// double the array and store in new array
gradesDoubled = doubleIt(grades);
input.close(); //close the input file
System.out.println("Enter the name of the file to output doubled grades to.");
File outFile = new File(keyboard.nextLine());
PrintWriter printer = new PrintWriter(outFile);
for(int n = 0; n < i; n++){
printer.print(gradesDoubled[n] + " ");
}
printer.close();
} // end of main method
private static double[] doubleIt(double[] numbers){
for(int i = 0; i < numbers.length; i++)
{
numbers[i] = numbers[i] * 2;
}
return numbers;
}
} // end of DoubleGrades class