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

Tell me what I am missing here.

P: 23
The Compiler says it okay. But then it doesn't finish. I have no idea what is missing?THis has to be a while loop.

Here is the code:import java.io.*;
import java.util.*;

public class Hulk

{


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;
counter = start;
counter = counter + 2;

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

Outfile.println();
}

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


Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. //Option #1
  2. while(firstNum % 2 == 0); // Problem!
  3. {
  4. start = firstNum + 1;
  5. start = firstNum;
  6. counter = start;
  7. counter = counter + 2;
  8.  
  9. Outfile.println("Odd integers between " + firstNum + " and " + secondNum + " are: ");
The semicolon after your while condition is incorrectly placed. What you have now is an infinite loop that continually checks if firstNum is even. This is because the while loop executes everything from its condition to the next semicolon in the same block (which is why you have the semicolon correctly placed after the } bracket). But you have a ; right after the condition, so the only thing looping is the condition.
Dec 9 '06 #2

P: 23
The semicolon after your while condition is incorrectly placed. What you have now is an infinite loop that continually checks if firstNum is even. This is because the while loop executes everything from its condition to the next semicolon in the same block (which is why you have the semicolon correctly placed after the } bracket). But you have a ; right after the condition, so the only thing looping is the condition.
I took it out and java still hangs up. the program just keeps running. Is there something else to look at?
Dec 9 '06 #3

DeMan
100+
P: 1,806
You are not doing anything to firstNum, that is, the condition of your while loop remains constant.

walk through the loop on a piece of paper, and consider when will the loop exit, and what will the variables be every step of the way?
Dec 10 '06 #4

P: 23
I got it thanks to those that responded.
Dec 10 '06 #5

Post your reply

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