By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

while loop question (Write to a file)

P: 2
// I have a block of code that runs okay but I need to write all the data to a file placing the appropriate label between each section of output. How would I do that?.

import java.io.*;

public class Ch5_PrExercise9
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{
int firstNum, secondNum;
int sumEven = 0;
int sumSquareOdd = 0;

char chCounter;

int counter;

int start;

System.out.println("Enter two numbers.");
System.out.println("First number must be less than or equal to "
+ "the second number you enter");
System.out.print("Enter the first number: ");
System.out.flush();
firstNum = Integer.parseInt(keyboard.readLine());
System.out.println();

System.out.print("Enter the second number: ");
System.out.flush();
secondNum = Integer.parseInt(keyboard.readLine());
System.out.println();

//Part a
if(firstNum % 2 == 0)
start = firstNum + 1;
else
start = firstNum;

System.out.println("Odd integers between " + firstNum + " and "
+ secondNum + " are: ");

for(counter = start; counter <= secondNum; counter = counter + 2)
System.out.print(counter + " ");

System.out.println();

//Part b
if(firstNum % 2 == 0)
start = firstNum;
else
start = firstNum + 1;

for(counter = start; counter <= secondNum; counter = counter + 2)
sumEven = sumEven + counter;

System.out.println("Sum of even integers between " + firstNum
+ " and " + secondNum + " = " + sumEven);

//Part c
System.out.println("Number\t\tSquare of Number");

for(counter = 1; counter <= 10; counter++)
System.out.println(counter + "\t\t" + counter * counter);

System.out.println();

//Part d
if(firstNum % 2 == 0)
start = firstNum + 1;
else
start = firstNum;

for(counter = start; counter <= secondNum; counter = counter + 2)
sumSquareOdd = sumSquareOdd + counter * counter;

System.out.println("Sum of the squares of odd integers between "
+ firstNum + " and " + secondNum
+ " = " + sumSquareOdd);
Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
// I have a block of code that runs okay but I need to write all the data to a file placing the appropriate label between each section of output. How would I do that?.

import java.io.*;

public class Ch5_PrExercise9
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{
int firstNum, secondNum;
int sumEven = 0;
int sumSquareOdd = 0;

char chCounter;

int counter;

int start;

System.out.println("Enter two numbers.");
System.out.println("First number must be less than or equal to "
+ "the second number you enter");
System.out.print("Enter the first number: ");
System.out.flush();
firstNum = Integer.parseInt(keyboard.readLine());
System.out.println();

System.out.print("Enter the second number: ");
System.out.flush();
secondNum = Integer.parseInt(keyboard.readLine());
System.out.println();

//Part a
if(firstNum % 2 == 0)
start = firstNum + 1;
else
start = firstNum;

System.out.println("Odd integers between " + firstNum + " and "
+ secondNum + " are: ");

for(counter = start; counter <= secondNum; counter = counter + 2)
System.out.print(counter + " ");

System.out.println();

//Part b
if(firstNum % 2 == 0)
start = firstNum;
else
start = firstNum + 1;

for(counter = start; counter <= secondNum; counter = counter + 2)
sumEven = sumEven + counter;

System.out.println("Sum of even integers between " + firstNum
+ " and " + secondNum + " = " + sumEven);

//Part c
System.out.println("Number\t\tSquare of Number");

for(counter = 1; counter <= 10; counter++)
System.out.println(counter + "\t\t" + counter * counter);

System.out.println();

//Part d
if(firstNum % 2 == 0)
start = firstNum + 1;
else
start = firstNum;

for(counter = start; counter <= secondNum; counter = counter + 2)
sumSquareOdd = sumSquareOdd + counter * counter;

System.out.println("Sum of the squares of odd integers between "
+ firstNum + " and " + secondNum
+ " = " + sumSquareOdd);
http://web.bvu.edu/faculty/schweller...riterDemo.java
Nov 17 '06 #2

P: 2
Hello,

That clears up writing to a file, but how do I include my data that typically would go to the screen such as System.out.println("Sum of the squares of odd integers between ") to end up in the file with the appropriate seperators?.











http://web.bvu.edu/faculty/schweller...riterDemo.java
Nov 17 '06 #3

10K+
P: 13,264
Hello,

That clears up writing to a file, but how do I include my data that typically would go to the screen such as System.out.println("Sum of the squares of odd integers between ") to end up in the file with the appropriate seperators?.
You write the data to the file instead of to the console.
Nov 18 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.