- Joined
- Apr 14, 2008
- Messages
- 3
- Reaction score
- 0
Hey, This is my current Problem:
Step 1 - Read from a file (2 Lines, Single digit on Each) - DONE
Example:
Step 2 - Determine All of the Even numbers in between those two numbers - DONE
Example:
Step 3 - Write the Even numbers To a File (The Same Format, 2 Lines, accept now have 5 numbers per like) - STUCK!
Example:
As you can see, i am currently Stuck on step 3. Where i am stuck is that, Every time that it goes through the loop for the first 5, it saves the number in the txt file like it should. However when it goes through the loop for the 2nd time, it Overwrites the numbers that are currently there. If you can help me, i would Greatly Appriciate it.
PS. I am still trying to Learn Java, so if you think you have a solution, could you try not to confuse me
. Thanks!
Step 1 - Read from a file (2 Lines, Single digit on Each) - DONE
Example:
Code:
5
25
Step 2 - Determine All of the Even numbers in between those two numbers - DONE
Example:
Code:
6
8
10
12
14
16
18
20
22
24
Step 3 - Write the Even numbers To a File (The Same Format, 2 Lines, accept now have 5 numbers per like) - STUCK!
Example:
Code:
6,8,10,12,14
16,18,20,22,24
PS. I am still trying to Learn Java, so if you think you have a solution, could you try not to confuse me

Code:
import java.io.*;
public class Assignment10 {
public static String GetString() {
String line = " ";
InputStreamReader input = new InputStreamReader (System.in);
BufferedReader reader = new BufferedReader (input);
try{
line = reader.readLine();
}catch(Exception e) { return line;}
return line;
}
public static void Output (String out) {
System.out.println(out);
}
public static void OutputSame (String out) {
System.out.print(out);
}
public static void ReadFile(String FileName, String content[],int num[]){
try{
FileReader inputfile = new FileReader(FileName);
BufferedReader inFile = new BufferedReader(inputfile);
String line = "";
line = inFile.readLine();
//content[counter] = line;
int counter = 0;
while(line !=null) {
content[counter] = line;
num[counter] = Integer.parseInt(content[counter]);
line = inFile.readLine();
counter = counter + 1;
}
inFile.close();
}
catch(IOException e){
}
}
public static void Write2File(String write){
try{
FileWriter WriteResult = new FileWriter("Assignment10Output.txt");
PrintWriter ResultOutput = new PrintWriter(WriteResult,true);
//print text to file
ResultOutput.println(write);
}
catch(IOException e){
}
}
public static void InitializeArray(String content[]){
for(int counter = 0; counter < 100; counter++) {
content[counter] = null;
}
}
public static void main (String args[]){
String content[];
int num[];
int OutputArray[];
content = new String[100];
num = new int[100];
OutputArray = new int[100];
InitializeArray(content);
ReadFile("Assignment10Input.txt",content,num);
Output ("*****************");
// For Testing Only - Outputs the the text that has been pulled from Assignment10Input.txt.
for(int counter = 0; content[counter] != null; counter++) {
Output(num[counter] + "");
}
int start = num[0];
int end = num[1];
int x = num[0];
int counter = 0;
Output ("*****************");
while(start<end + 1){
if(x % 2 == 0){
Output(x + " - Even");
OutputArray[counter] = x;
counter = counter + 1;
}else if(x % 2 == 1){
//Output(x + " - Odd");
}else{}
start = start + 1;
x=x+1;
}
Output ("*****************");
// for(int i=1;i<counter;i=i+1){
// Output (OutputArray[i] + "");
// }
//Output ("*****************");
int counter2 = 0;
while(counter2 < end + 1){
if(counter2 < 5){
Write2File(OutputArray[counter2] + "-");
counter2 = counter2 + 1;
}else if(counter2 > 5 && counter <= 10){
Write2File(OutputArray[(counter2 + 5)] + ",");
counter2 = counter2 + 1;
}else{
counter2 = counter2 + 1;
}
}
}
}