Well, I am unaware as to whether you're a student in a Java class, either as part of an AP subset or something along those lines, but seeing as you are probably trying to learn, you do have some bad coding habits, and you're passing arrays into methods and filling them into methods, which is a bad habit, and will not work for non static variables. (Your variables are static because of being in a static method).
Anyhow I provide you an example of what you have, except in a shorter neater way. It is simply as an example of how things can get easier, another approach to it.
Code:
import static java.lang.System.*; // allows me to just go out.println();
import java.io.*;
import java.util.ArrayList; // for ArrayLists, not super efficient but will work for this
import java.util.Scanner;
public class Assignment10 {
public static String getString() {
Scanner input = new Scanner(System.in);
String line = "";
line = input.nextLine();
input.close();
return line;
}
// This method will simply return an array containing integers and it can cause the FileNotFoundException
public static ArrayList<Integer> readFile(String file) throws FileNotFoundException {
Scanner input = new Scanner(new File(file));
ArrayList<Integer> ret = new ArrayList<Integer>();
while(input.hasNext()) {
ret.add(Integer.parseInt(input.nextLine()));
}
input.close();
return ret;
}
// I have rewritten your writing code into this method
public static void writeToFile(ArrayList<Integer> ints) {
/*
* To be implemented
*/
}
public static void main(String args[]) {
ArrayList<Integer> inputted = new ArrayList<Integer>();
ArrayList<Integer> calculated = new ArrayList<Integer>();
try {
inputted = readFile("Assignment10Input.txt");
} catch(FileNotFoundException e) {
out.println("Error: File wasn't found");
exit(0); // same as System.exit(0); due to static import
}
for(int number: inputted)
out.println(number);
int start = inputted.get(0);
int end = inputted.get(1);
while(start<=end) {
if(start % 2 == 0) {
out.println(start + " - Even");
calculated.add(start);
start+=2; // now we now it is even, so if we just add two we will get the next even number
} else
start++; // simply add one as it is odd
}
writeToFile(calculated);
}
}
Now onto printing them all to a file, I see you already have a PrintWriter setup, with your way of doing things, and if you were to do the same with mine it would look significantly different than yours, but here is what you need to do, and I'll try to explain it off of how your code looks.
You will need to create a counter variable like your counter2. Then, what you need to do is a simple check every time it is on the fifth number, which can be done somewhat like this:
if counter2 < 4
write the number to the file
if counter2 == 4
write the number to file, with a '\n' behind it
set counter2 to zero
This should correctly write it out into a file for you, the reason for 4, is if you start counter2 at zero, you will have 5 numbers once you hit 4:
0 1 2 3 4 = 5 numbers
Anymore questions feel free to ask them here, just keep in mind we don't necessarily like to do your homework for you, if that's the case.