Java I/O Assistance + Loop
I'm trying to make this program write file numbers.dat that's accomplished.
I'm also trying to make it write all even numbers into the file and close the file.
Then I'm trying to make it re open file and append all odd numbers 1-100 and finally close file.
But for some reason I CANNOT Get the output as I want. I know where the error lays I just can't seem to grasp how to fix it... I've commented as I went along and sought feedback from peers to no luck. I now come here in hopes of assistance.
the problem is within my loop I believe I just cant wrap my head around how to get a proper display.
Current Output shows:
Numbers.Dat file name and type
Within File Shows:
2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550,
Clearly NOT 2,4,6,8,10,12,14,.,.,.Etc
print("package textFileIO;
import java.io.*;
import java.util.*;
//class definition
public class textFileIO {
//The main function
public static void main(String args[]) {
//This declares a file to open.
File outFile = new File("numbers.dat");
int sumEven = 0;
int sumOdd = 0;
//File IO requires a try/catch block to prevent the program from crashing
try {
//a buffered writer is used to allow us to write to the file.
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
//This for loop handles adding all of the numbers together
for(int i = 1; i < 100; i += 2) {
sumEven += i + 1;
writer.write("" + sumEven + ", ");
}
//adding a new line and closing the file
writer.newLine();
writer.close();
//A buffered Reader is used for reading a new file.
BufferedReader reader = new BufferedReader(new FileReader(outFile));
//Because I only add a new line at the end, I only need to read the first line.
System.out.println(reader.readLine());
//I need to write again, so I close the reader and open the writer.
reader.close();
//you need to create another object to append to the file
//writer.open();
BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile));
//This for loop is identical to the previous one, except for odd numbers
for(int i = 1; i < 100; i += 2) {
sumOdd += i;
writer2.write("" + sumOdd + ", ");
}
writer.newLine();
writer.close();
//The same here. You need to create another reader
//reader.open();
BufferedReader reader2 = new BufferedReader(new FileReader(outFile));
System.out.println(reader2.readLine());
}
catch (Exception e) {
}
}
}");
With all due respect,
Brandon
View Answers
December 1, 2010 at 12:10 PM
Hi Friend,
We have modified your code:
import java.io.*;
import java.util.*;
public class textFileIO {
public static void main(String args[]) {
File outFile = new File("numbers.dat");
int sumEven = 0;
int sumOdd = 0;
try{
BufferedWriter writer1 = new BufferedWriter(new FileWriter(outFile,true));
for(int i = 1; i <= 100; i ++) {
if(i%2==0){
writer1.write(""+i+"");
writer1.newLine();
}
}
writer1.close();
BufferedReader br1 = new BufferedReader(new FileReader(outFile));
String str1="";
while ((str1 = br1.readLine()) != null) {
System.out.println(str1);
}
BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile,true));
for(int i = 1; i <= 100; i ++) {
if(i%2!=0){
writer2.write(""+i+"");
writer2.newLine();
}
}
writer2.close();
BufferedReader br2 = new BufferedReader(new FileReader(outFile));
String str2="";
while ((str2 = br2.readLine()) != null) {
System.out.println(str2);
}
}
catch (Exception e) {}
}
}
Thanks
Related Tutorials/Questions & Answers:
Java I/O Assistance + LoopJava I/
O Assistance + Loop I'm trying to make this program write... peers to no luck.
I now come here in hopes of
assistance.
the problem is within my
loop I believe
I just cant wrap my head around how to get a proper
Advertisements
for loop in javafor
loop in java 1
1 2 3
1 2 3 4
1 2 3 4 5
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 10
using for
loop in
java
Java Number Pattern
1
1...(String[] args){
for(int
i=1;i<=5;
i++){
for(int j=1;j<
i+1;j
for loop in javafor
loop in java 54321
4321
321
21
1
using for
loop or nested
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
using for
loop or nested... 8 9
6 7 8 9 10
all for
loop code plz friends guide me
for loop in javafor
loop in java 54321
4321
321
21
1
using for
loop or nested
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
using for
loop or nested... 8 9
6 7 8 9 10
all for
loop code plz friends guide me
for loop in javafor
loop in java 54321
4321
321
21
1
using for
loop or nested
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
using for
loop or nested... 8 9
6 7 8 9 10
all for
loop code plz friends guide me
for loop in javafor
loop in java 54321
4321
321
21
1
using for
loop or nested
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
using for
loop or nested... 8 9
6 7 8 9 10
all for
loop code plz friends guide me
for loop in javafor
loop in java 54321
4321
321
21
1
using for
loop or nested
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
using for
loop or nested... 8 9
6 7 8 9 10
all for
loop code plz friends guide me
For loop javaFor
loop java 1
1 2 3
1 2 3 4
1 2 3 4 5
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 10
looploop
i want to write my name(inder) through
loop in
java using star
for loop it is infinite
loop.
Java provides several
loop structures for iteration. Those...++)
{
System.out.println("Tick" +
i);
}
}
Here the
loop is executed 10 times from 0 to 10. At first when control enters the
loop the variable '
i For Loop in Java
For
Loop in
Java -
Java For
Loop Examples & Syntax
The for
loop is the type of looping construct. It also works as while
loop
construct but it provide the initialization
While loop - Java BeginnersWhile loop Given a number, write a program using while
loop...;
}
System.out.println("Reverse Number : " + rev);
}
}
For more information on
Java Examples visit to :
http://www.roseindia.net/
java/beginners/Palindrome.shtml
LOOP PROGRAM - Java BeginnersLOOP PROGRAM Hi all,
Someone please help this code.
I need a
Java program that uses looping to toss a coin over and over until it comes up head 10 times. The program should also record the number of tails recorded
Nested Loop - Java BeginnersNested Loop Hi,
Can anyone tell me example of Nested
loop in
Java?
Thanks
Hi friend,public class NestedExample{ public static void main(String[] args){ /** Print Pattern
I */ System.out.println("
looploop what is the difference b/w do while,while and for
loop..with example
For loopFor loop Hi!
Please explain the following
loop:
for(
i=0;i<a[j];
i++)
THANX!!
Please reply as soon as possible
for loopfor loop how to performs the for
loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex.
1
12
123
1234
For Loop Statement in java 7For
Loop Statement in
java 7
In this section, we will discuss about for
loop in
java 7. This is one type of
loop statement.
For
Loop Statements...
Here is simple for
loop example. First we initialized the variable
i =1 and
loop problem - Java Beginnersloop problem
I trying to write a program which prints out even numbers, I've got that part, however,
I need to only have 5 numbers per line.
ex: 2 4 6 8 10
12 14 16 18 20
I tried using a counter, but I'm missing
loop problem - Java Magazineloop problem import java.util.Scanner;
class mychoice{
public... to main menu to select new item.
//what
loop to use?
break...");
Statement st=con.createStatement();
int
i=st.executeUpdate("insert
Loop to understand a
loop
my quesiton are:
what du
i do wrong in my code since... dk Hi,
Please check the for
loop once again:
for (int
i... here :
i = stop; but it should be
i <= stop;
The correct
loop would
for loop in java example
.style1 {
color: #0000FF;
}
for
loop in
java example
We are going to discuss about for
loop in
java example. The for
loop
statement has type
loop control statement. We first
initialize the variable. After that check
For LoopFor Loop Write a program using a for
loop that computes... the user enters.
This is an example of my upcoming quiz.
I need help on knowing... + 4/2 + 5/2 + ... N/2
for(int
i=1;i<=n;
i++){
sum+=
i looploop strong textHello can someone help me with this problem,
I need to output the following code using a
loop the code is as follows with the user inputs in inverted commas,thanks.
ENTER CODE(XX to Stop)
CODE:"D24"
QUANTITY:"2
for loopfor loop using only two for
loop
how can print below numbers
1...){
for(int
i=1;i<=9;
i++){
for(int j=1;j<=
i;j++){
System.out.print(
i);
}
System.out.println
for loopfor loop using only two for
loop
how can print below numbers
1...){
for(int
i=1;i<=9;
i++){
for(int j=1;j<=
i;j++){
System.out.print(
i);
}
System.out.println
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7
Square pattern in java using for loopSquare pattern in
java using for loop please explain the logic to program the following using for
loop in
Java:
1 3 5 7 9
3 5 7 9 1
5 7 8 1 3
7 9 1 3 5
9 1 3 5 7