
October 7th, 2004, 10:01 PM
#1
while, for, and do while loops...odd and even numbers with tokenizer..help please!
Hi, I'm using Blue J for my programming. I have been fine in the class up to this point. This is my first Java class and the teacher and book is no help at all. Im used to visual basic.net. I need to turn in 3 programs.
write a program that uses a while loop to perform these steps
a. prompt user to input 2 integers:firstnum and secondNum (firstNum has to be less than secondNum)
b. ouput the sum of all even numbers between firstNum and secondNum
c. Output the sum of the square of odd numbers between firstnum and secondNum.
I am also supposed to do these steps in a for loop, and a do while loop.
I have no idea what to do. I am supposed to use the JoptionPane format and not the console for all my programs. My book doesnt even show how to do this with the tokenizer which i think im supposed to use. This is as far as i have gotten:
Code:
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class WhileLoop
{
public static void main(String[] args) throws IOException
{
String firstNumberInput;
String secondNumberInput;
StringTokenizer tokenizer;
int firstNum;
int secondNum;
firstNumberInput= JOptionPane.showInputDialog ("Enter a whole number");
secondNumberInput= JOptionPane.showInputDialog ("Enter a second whole number that has a higher value than the first number you entered");
firstNum= Integer.parseInt(firstNumberInput);
secondNum= Integer.parseInt(secondNumberInput);
please help..as you can see i havent gotten very far. I am at a complete loss.

October 8th, 2004, 07:40 AM
#2
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
Look up the semantics of each loop type; this will tell you how they work (Google will help if your textbook is that bad). Then you've got to figure out how to use each loop to do the task required.
It seems pretty obvious that given a first number and a second number and a task to sum all the even numbers between them using a loop, you are expected to loop through the numbers from the first to the second (or viceversa!), adding each even number to an eventotal as you encounter it, and squaring and adding odd numbers to an oddtotal.
A while loop works like this:
while (conditionistrue) {
dosomeprocess;
}
To process the numbers between the first and the second, you start with the number after the first number (first + 1), and finish when you've reached the second number. The process will deal with one number at a time, and will add the current number (or it's square) to a total. So you'll want to use a number which will start with the value of the first number input, and will be incremented each time round the loop. To stop the loop when it reaches the second number, you'll want to make the condition test whether the current number is less than the second number. Something like this:
Code:
int current = first + 1;
while (current < second) {
// if current number is even, add to even total here
// if current number is odd, square it and add to odd total here
current++; // increment current to the next number
}
// print out totals here
You'll also want variables to hold the odd and even totals.
The other loops will have slightly different ways of setting up, incrementing, and testing the current number, but they do the same thing.
There is nothing so useless as doing efficiently that which should not be done at all...
P. Drucker
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

October 9th, 2004, 03:57 PM
#3
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
thanks for the help but im still getting no where. Your code did help a little. What is my condition in the whil loop? is it what i have? How do i determine if it is even..i know i use the mod operater but im not sure how to do it. Any code would be great...I have to see it to learn it.

October 9th, 2004, 07:16 PM
#4
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
A little mathematical background, an even number is any number that can be divided evenly by 2. You can use a for loop to print out the numbers:
Code:
// To found out how many times the loop executes, take secondNum  firstNum
// If the 2nd statement was instead: loopCount <= secondNum then the loop
// executes secondNum  firstNum + 1 times
// If the loop increment was something other then one, then you would divide
// the above result by the amount of the increment:
// for (...;loopCount < secondNum; loopCount += 5 > (secondNum  firstNum)/5 times
// for (...;loopCount <= secondNum; loopCount += 5 > (secondNum  firstNum + 1)/5 or
for (int loopCount = firstNum; loopCount < secondNum; loopCount++)
{ //..determine if loopCount is an even number
// if it is, print loopCount
}
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

October 10th, 2004, 08:20 AM
#5
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
I have to see it to learn it.
Not true, unfortunately. You may remember what you see, but it will only be a specific solution to a specific problem. The really important part is learning and practicing how you arrive at the solution  how you solve problems. The best way to learn something is to figure it out and do it for yourself. In fact, it's the only way to learn how to solve problems, which is the main benefit of these programming exercises. You can look almost everything up online or in a book, except the skill of solving problems. Don't be intimidated by looking at the whole problem all at once, break it down into logical chunks and try to work out each one, then you can figure out how to put them together, e.g you might approach the current problem like this:
You want to print the totals, so you'll need some total variables to accumulate the totals. You want to process each number between the start and finish number with a loop, so you're best off doing one number at a time before moving on to the next. So you'll need a variable to hold the current number while you mess with it. You'll be doing the same processing on each number, so you can reuse the same piece of code, and you might as well reuse the variable holding the current number also. If you loop through the numbers one at a time, the processing code must go inside the loop, and the variable with the current number can be changed to the next number each time around. One or other of the totals will be incremented for each number processed (each time round the loop), so they must be declared first and set to zero, before the loop code, and incremented inside the loop... etc. Small logical steps.
The mod operator gives you the remainder after dividing by a specified number. If you divide by 2, the remainder will always be 0 or 1. You should be able to figure out which one means the number was odd and which means it was even. This is how the mod operator works:
Code:
int remainder = dividend % divisor;
For example, in the case where 7 is the dividend and 2 the divisor, the result (remainder) will be 1. If the dividend was 8 and the divisor 2, the result would be 0.
I hear and I forget; I see and I remember; I do and I understand...
Chinese proverb
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

October 10th, 2004, 10:10 PM
#6
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
Code:
String firstNumberInput;
String secondNumberInput;
String outputMessage;
int firstNum=0;
int secondNum=0;
int counter=0;
int sum=0;
int current=0;
while (firstNum<=secondNum){
firstNumberInput= JOptionPane.showInputDialog ("Enter a whole number");
secondNumberInput= JOptionPane.showInputDialog ("Enter a second whole number that has a higher value than the first number you entered");
firstNum= Integer.parseInt(firstNumberInput);
secondNum= Integer.parseInt(secondNumberInput);
if (firstNum % 2 != 0){
++firstNum;
++counter;
}
sum= firstNum;
}
outputMessage= "Sum of evens: " +firstNum;
JOptionPane.showMessageDialog(null, outputMessage, "Loop", JOptionPane.INFORMATION_MESSAGE);
}
}
could somebody fix this for me? I get a runtime error plus i cant get any output and i still need to do the odd number thing as stated above! THanks for the help so far!

October 10th, 2004, 11:28 PM
#7
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
Use the little code snippet I posted earlier and follow the comments:
Code:
//... code to read in firstNum and secondNum
for (int loopCount = firstNum; loopCount < secondNum; loopCount++)
{ //..determine if loopCount is an even number
// if it is, print loopCount
}
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

October 14th, 2004, 03:52 PM
#8
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
hey Rage,
I think you and I are in the same class, SERIOUSLY! Maybe we can help eachother out. I have the same problem, " ex. 8,9,& 10". I've been having trouble getting mine to work also. I'm pretty sure you don't need to use a tokenizer unless your using BufferReader, or keyboard readline. Here is my code, see if you can see my problem, or if anyone else can.
Code:
/**
* whileLoop project Ex8
*
*
* 12 Oct 04
*/
import javax.swing.JOptionPane;
public class whileLoop
{
public static void main(String[] args)
{
int sum;
int firstN;
int secondN;
int counter;
int val;
int squares;
String inputStr;
String outputStr;
inputStr = JOptionPane.showInputDialog
("Please enter two integers.\n"
+ "** NOTE: First integer must be less than second integer.\n"
+ "Enter first integer:");
firstN = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog
("Enter second integer:");
secondN = Integer.parseInt(inputStr);
outputStr = "Your two integers were " + firstN + " and " + secondN;
val = firstN % 2;
sum = 0;
counter = 0;
evens = 0;
if(firstN < secondN)
{
if(val == 0)
counter = firstN;
else
counter = firstN + 1;
while(counter < secondN)
{
// this is the beginning of my problem
sum = sum + counter;
counter = counter + 2;
// I can't seem to get the sum to come out like i want it to. I
// don't know what i'm doing wrong
}
outputStr = "The sum of the even integers are: " + sum;
JOptionPane.showMessageDialog(null,outputStr,"whileloop",
JOptionPane.PLAIN_MESSAGE);
}
else
{
outputStr = "First number must be lower than second number.";
JOptionPane.showMessageDialog(null,outputStr,"whileLoopError",
JOptionPane.ERROR_MESSAGE);
}
System.exit(0);
}
}
Last edited by cjard; October 16th, 2004 at 06:59 PM.
Reason: use [CODE] tags.. http://www.codeguru.com/forum/misc.php?do=bbcode

October 14th, 2004, 11:34 PM
#9
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
Is my hint really that subtle? Maybe a similiar example:
PHP Code:
class NumberSumer
{
public static void main(String[] args)
{ // get the user to enter a number >= 0
String input = JOptionPane.showInputDialog("Enter a positive integer: ");
int val = Integer.parseInt(input);
// the above code primes the loop below
// if the user entered a value less then 0, keep bothering them until
// they enter the requested input
while (val < 0)
{ input = JOptionPane.showInputDialog(null, "The number must be positive. \n"
+ "Enter a positive integer: ");
val = Integer.parseInt(input);
}
System.out.print("The sum of all the numbers between 0 and " + val
+ ", inclusive, is: ");
int sum = 0;
for (int count = 0; count <= val; count++)
{ sum += count; }
System.out.println(sum);
System.exit(0);
}
}
/* Entering a value of ten prints this output:
The sum of all the numbers between 0 and 10, inclusive, is: 55
Press any key to continue...
*/
This should hopefully give you both some ideas on how to solve your problem. It should take about 3 lines of code to determine if a number is prime and to sum them up.
Last edited by cma; October 14th, 2004 at 11:41 PM.
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

October 16th, 2004, 06:22 PM
#10
Re: while, for, and do while loops...odd and even numbers with tokenizer..help please!
thanks for all the help!
hey groundrat, you go to sms? what time you in 260 on what days?
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
