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.

Why is option #3 caught in a loop - I'm lost

P: 23
import java.io.*;
import java.util.*;

public class Chunk

{


public static void main (String[] args) throws IOException


{
BufferedReader inFile =
new BufferedReader(new FileReader("C:\\Chunk.dat"));
PrintWriter Outfile =
new PrintWriter(new FileWriter("C:\\Chunk.out"));
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));

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(stdin.readLine());
System.out.println();

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

//Option #1
while(firstNum % 2 == 0)
start = firstNum + 1;
start = firstNum;

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

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

Outfile.println();

//Option #2
while(firstNum % 2 == 0)
start = firstNum;
start = firstNum + 1;

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

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

//Option #3
while(firstNum % 2 == 0);
start = firstNum +1;

Outfile.println("Number\t\tSquare of Number");
for(counter = start; counter <= secondNum; counter = counter)
Outfile.println(counter + "\t\t" + counter * counter);

Outfile.println();

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

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

Outfile.println("Sum of the squares of odd integers between " + firstNum + " and " + secondNum + " = " + sumSquareOdd);

inFile.close();
Outfile.close();
}
}
Dec 8 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
probably the problem is the ; on the end of
Expand|Select|Wrap|Line Numbers
  1. while(firstNum % 2 == 0);
  2.  
which would put the code into a loop

not sure about your logic - should the while() statements be if statemets?
Dec 8 '06 #2

DeMan
100+
P: 1,806
because you have a semicolon behind it, that means it just loops itself.

If statements and loops execute the first statement after them, terminated by ; Theoretically 'do nothing' is a statement. For clarity (and ease of extension later). The statement can also be {} (without a semicolon) which basically says 'do this statemnt list'. I like to use this notation even if I only have 1 statement, because it is easy to follow, and helps avoid the type of error your getting (as well as making it more intuitive if you add more statements later).
Dec 9 '06 #3

Post your reply

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