473,756 Members | 9,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

while loop question (Write to a file)

2 New Member
// 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 InputStreamRead er(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.prin tln("Enter two numbers.");
System.out.prin tln("First number must be less than or equal to "
+ "the second number you enter");
System.out.prin t("Enter the first number: ");
System.out.flus h();
firstNum = Integer.parseIn t(keyboard.read Line());
System.out.prin tln();

System.out.prin t("Enter the second number: ");
System.out.flus h();
secondNum = Integer.parseIn t(keyboard.read Line());
System.out.prin tln();

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

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

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

System.out.prin tln();

//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.prin tln("Sum of even integers between " + firstNum
+ " and " + secondNum + " = " + sumEven);

//Part c
System.out.prin tln("Number\t\t Square of Number");

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

System.out.prin tln();

//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.prin tln("Sum of the squares of odd integers between "
+ firstNum + " and " + secondNum
+ " = " + sumSquareOdd);
Nov 16 '06 #1
3 3777
r035198x
13,262 MVP
// 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 InputStreamRead er(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.prin tln("Enter two numbers.");
System.out.prin tln("First number must be less than or equal to "
+ "the second number you enter");
System.out.prin t("Enter the first number: ");
System.out.flus h();
firstNum = Integer.parseIn t(keyboard.read Line());
System.out.prin tln();

System.out.prin t("Enter the second number: ");
System.out.flus h();
secondNum = Integer.parseIn t(keyboard.read Line());
System.out.prin tln();

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

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

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

System.out.prin tln();

//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.prin tln("Sum of even integers between " + firstNum
+ " and " + secondNum + " = " + sumEven);

//Part c
System.out.prin tln("Number\t\t Square of Number");

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

System.out.prin tln();

//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.prin tln("Sum of the squares of odd integers between "
+ firstNum + " and " + secondNum
+ " = " + sumSquareOdd);
http://web.bvu.edu/faculty/schweller...riterDemo.java
Nov 17 '06 #2
dnatzic
2 New Member
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.prin tln("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
r035198x
13,262 MVP
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.prin tln("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

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

Similar topics

4
15956
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement. The while statement is as follows. while(infile.peek() != EOF) { infile.getline(temp1, max); };
3
3108
by: .Net Sports | last post by:
I want to include some asp if statements inside a do until loop, predicated on the recordset going until EOF. I do not have any < % %> delimiters inside the include file. The below doesnt show the character I want to display: <% do until rs.eof response.write rs("position") <!-- #include file="inc_.asp" --> loop %>
3
2728
by: AutoShutdown | last post by:
How can I change a FOR EACH loop to a DO WHILE loop? I have this code... For Each objFile In objFolder.Files response.write objFile.Name Next
5
1519
by: Patrick | last post by:
I'm trying to while loop using a flat txt file and write the values out into a form. I have 50-58 values to echo. Am I going to have to echo each line of html. Or is there a way to make this work between the php. Right now it wants to write the whole table over and over. Is my logic screwed up? I am not a programmer as such but can get by. I have something like this which is an abbreviated version. Any help would be appreciated. Thanks,...
3
2513
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do. I have it set in a way whenever I click the PLAY button, the PLAY button is suppose to hide and the PAUSE button is suppose to appear. This does not happen until the complete while iteration has finished. My goal is to Play and Pause at any time I...
0
9482
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9292
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9901
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9728
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8733
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.