I'm new to Dynamic Programming so I tried to create a file and write it onto Array.txt in the download section. The logic is:
If the Array is longer than the current array: Extend the array and copy to old array onto the new one
I, however, can't find a way to actually copy the section of the old array onto the new one. The way that I am using right now is
System.arraycopy()
Here is the code: (Remember to swap out YOURUSERNAME with your current username on the computer to avoid getting an error)
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Fibonacci {
static void createFile() {
try {
File Array = new File("C:\\Users\\YOURUSERNAME\\Downloads\\Array.txt");
if (Array.createNewFile()) {
System.out.println("File created: " + Array.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
static void fileWrite(int[] array, int num) {
try {
FileWriter myWriter = new FileWriter("C:\\Users\\YOURUSERNAME\\Downloads\\Array.txt");
if (num <= array.length) {
int[] newA = new int[array.length];
System.arraycopy(array, 0, newA, 0, array.length);
myWriter.write(Arrays.toString(newA));
myWriter.close();
}
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
static int[] fileRead(int[] Array1) {
try {
StringBuilder dataTotal = new StringBuilder();
File Array = new File("C:\\Users\\YOURUSERNAME\\Downloads\\Array.txt");
Scanner myReader = new Scanner(Array);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
dataTotal.append(data);
}
myReader.close();
System.out.println(dataTotal);
dataTotal.deleteCharAt(0);
dataTotal.deleteCharAt(dataTotal.length() - 1);
String[] array1 = dataTotal.toString().split(", ");
int[] array = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
array[i] = Integer.parseInt(array1[i]);
}
return array;
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
return null;
}
}
static int[] arrayCreator(int num) {
return new int[num];
}
static int fib(int num, int[] array1) {
int[] array = fileRead(array1);
if (num == 1 || num == 2) {
return 1;
}
else {
assert array != null;
if(array[num - 1] > 0) {
return array[num - 1];
} else {
array[num - 1] = fib(num - 1, array1) + fib (num - 2, array1);
fileWrite(array, num);
}
}
return array[num - 1];
}
public static void main(String[] args) {
int num = 10;
int[] array = arrayCreator(num);
createFile();
fileWrite(array, num);
System.out.println(fib(num, array));
}
}
fileWrite()method should only be responsible for writing an array to file, it should not be doing any array copying. Your code should be building the array in memory, and only callingfileWrite()once at the end. Take a step back and re-think what you're doing.writeFileshould do just that, white the content of an array to file. It shouldn't be responsible for copying values from one array to another.