While loops odd numbers

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 8606 Views - Last Post: 23 November 2011 - 09:36 PM Rate Topic: -----

#1 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

While loops odd numbers

Posted 20 November 2011 - 03:22 PM

package whileloops;

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;

public class WhileLoops {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        //Declare Variables
        int firstNum, secondNum, tempNum, oddNums, evenNums;
        String outputStr, outputStr1, outputStr2, outputStr3, outputStr4, sMessage, pMessage,
                numOne, numTwo;

        JOptionPane.showMessageDialog(null, "First number must be less than "
                + "second number!", "Title", JOptionPane.INFORMATION_MESSAGE);

        numOne = JOptionPane.showInputDialog("Enter first number: ");
        numTwo = JOptionPane.showInputDialog("Enter second number: ");

        firstNum = Integer.parseInt(numOne);
        secondNum = Integer.parseInt(numTwo);
        tempNum = firstNum;

        oddNums = 0;
        evenNums = 0;

        if (firstNum <= secondNum) {
            outputStr1 = "First number: " + firstNum + "\n"
                    + "Second number: " + secondNum;

            JOptionPane.showMessageDialog(null, outputStr1,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Please enter first number "
                    + "as less than second", "Error Message",
                    +JOptionPane.ERROR_MESSAGE);

            numOne = JOptionPane.showInputDialog("Enter first number: ");
            numTwo = JOptionPane.showInputDialog("Enter second number: ");

            firstNum = Integer.parseInt(numOne);
            secondNum = Integer.parseInt(numTwo);

            outputStr2 = "First number: " + firstNum + "\n"
                    + "Second number: " + secondNum;

            JOptionPane.showMessageDialog(null, outputStr2,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
        }

        while (tempNum < secondNum) {
            if (tempNum % 2 != 0) {
                oddNums = tempNum;
            }
            oddNums++;

            break;
        }

        outputStr3 = "Odd numbers between " + firstNum + " and " + secondNum
                + "\n" + " : " + oddNums;

        JOptionPane.showMessageDialog(null, outputStr3,
                "Message", JOptionPane.INFORMATION_MESSAGE);

        outputStr4 = "Sum of even numbers between " + firstNum + " and "
                + secondNum + "\n" + " : " + evenNums;

        System.exit(0);
    }
}


i am trying to get the while loop to display all of the odd numbers between firstNum and secondNum. i would like it to display in a JOptionPane like the rest of my code does.

Is This A Good Question/Topic? 0
  • +

Replies To: While loops odd numbers

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: While loops odd numbers

Posted 20 November 2011 - 03:29 PM

SO what is the problem with your code?
And what a break statement is doing if you want to print all numbers?
        while (tempNum < secondNum) {
            if (tempNum % 2 != 0) {
                oddNums = tempNum;
            }
            oddNums++;

            break; // do you know what this statement do?
        }

This post has been edited by smohd: 20 November 2011 - 03:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 03:40 PM

the break i meant to take out and i want to display all of the numbers between firstNum and secondNum(in this case tempNum = firstNum) for example
firstNum = 1;
second num = 10;



i want it to display 3, 5, 7, 9.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,099
  • Joined: 19-March 11

Re: While loops odd numbers

Posted 20 November 2011 - 04:00 PM

Get the first odd number in the range. Repeatedly add two to it. Stop when you go out of range.

To display in a JOptionPane, collect the numbers in a String and write it to the OptionPane.

Implementation is left up to you.

This post has been edited by jon.kiparsky: 20 November 2011 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: While loops odd numbers

Posted 20 November 2011 - 04:00 PM

Then your while loop should not be so. If you are expecting to print them all after the loop in JOptionPane, you may need a string to append them all, and your loop will take the following view:
 while (tempNum < secondNum) {
    if (tempNum % 2 != 0) 
       //append the tempNum to a result string

    tempNum++; //this is the one to be incremented


Other things to note:
- You have to look again of your if else statement if user enters incorrect order, what if user enter wrong order again?
- You assigned tempNum before if else, this will give an incorrect result if user make mistake ffor the first time
Was This Post Helpful? 2
  • +
  • -

#6 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 04:43 PM

package whileloops;

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;

public class WhileLoops {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        //Declare Variables
        int firstNum, secondNum, counter, evenNums;
        String outputStr, oddNums, outputStr1, outputStr2, outputStr3, outputStr4, sMessage, pMessage,
                numOne, numTwo;

        JOptionPane.showMessageDialog(null, "First number must be less than "
                + "second number!", "Title", JOptionPane.INFORMATION_MESSAGE);

        numOne = JOptionPane.showInputDialog("Enter first number: ");
        numTwo = JOptionPane.showInputDialog("Enter second number: ");

        firstNum = Integer.parseInt(numOne);
        secondNum = Integer.parseInt(numTwo);

        oddNums = " ";
        evenNums = 0;

        if (firstNum <= secondNum) {
            outputStr1 = "First number: " + firstNum + "\n"
                    + "Second number: " + secondNum;

            JOptionPane.showMessageDialog(null, outputStr1,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Please enter first number "
                    + "as less than second", "Error Message",
                    +JOptionPane.ERROR_MESSAGE);

            numOne = JOptionPane.showInputDialog("Enter first number: ");
            numTwo = JOptionPane.showInputDialog("Enter second number: ");

            firstNum = Integer.parseInt(numOne);
            secondNum = Integer.parseInt(numTwo);

            outputStr2 = "First number: " + firstNum + "\n"
                    + "Second number: " + secondNum;

            JOptionPane.showMessageDialog(null, outputStr2,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
        }

        counter = firstNum;

        while (counter < secondNum) {

            if (firstNum % 2 != 0) {
                oddNums = oddNums + counter + " ";
                counter++;
            } else if (firstNum % 2 == 0) {
                counter++;
                oddNums = oddNums + counter + " ";
            }
        }

        outputStr3 = "Odd numbers between " + firstNum + " and " + secondNum
                + "\n" + " : " + oddNums;

        JOptionPane.showMessageDialog(null, outputStr3,
                "Message", JOptionPane.INFORMATION_MESSAGE);

        outputStr4 = "Sum of even numbers between " + firstNum + " and "
                + secondNum + "\n" + " : " + evenNums;

        System.exit(0);
    }
}

edited code it fixes most of it but if i enter 1 and 10 it outputs 1 2 3 4 5 6 7 8 9 instead of 3 5 7 9. i know its in this
if (firstNum % 2 != 0) {
                oddNums = oddNums + counter + " ";
                counter++;

any idea?
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,099
  • Joined: 19-March 11

Re: While loops odd numbers

Posted 20 November 2011 - 04:46 PM

if (firstNum % 2 != 0) {
                oddNums = oddNums + counter + " ";
                counter++;



The first rule of debuging: don't ask why it isn't doing what you want to do, ask what it's really doing. You've picked the right chunk of the code, now look at it carefully and figure out what each step does. Particularly, under what circumstances is the if condition true?

More importantly, why are you doing all of these modulo tests? Once you have one odd number, n, the next odd number is n+2. No testing needed, except to set your starting point.

This post has been edited by jon.kiparsky: 20 November 2011 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

#8 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 06:37 PM

well i did to modulo test because the first one is suppose to be if i input an odd number first as in 1 3 5 7 9 11 etc... the second is if the number is a even number as in 2 4 6 8 10 12 etc... the second one works out fine if i input 2 10 it outputs 3 5 7 9 but the first part of the if statement if i input 1 and 10 it outputs 1 2 3 4 5 6 7 8 9. I'm trying to debug that first part of the code i quoted.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,099
  • Joined: 19-March 11

Re: While loops odd numbers

Posted 20 November 2011 - 06:44 PM

Right, so you get two numbers from the user. One of them is less than the other, and you start with that one. If that number is even, you add one to it. Now you have an odd number. From there, you no longer need to test whether the number is even, because you know that if you add two to it, it's odd.



To find the bug in your code, just figure out what it does. Not what you meant it to do, what it does.

You have an if. When is that if true? When will the body of that if be executed?
Was This Post Helpful? 1
  • +
  • -

#10 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 06:55 PM

Ok with your advice i got it working how i wanted it to other than one anoying bug,
when my counter = 1 and secondNum = 10 the output is 3 5 7 9 11. i understand why its doing this, its because 9 < 10 so it adds 2 more to the counter with the result being 11.

        while (counter < secondNum) {

            if (firstNum % 2 == 0) {
                oddNums = oddNums + counter + " ";
                counter++;
            } else {
                counter +=  2;
                oddNums = oddNums + counter + " ";
            }
        }


Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,099
  • Joined: 19-March 11

Re: While loops odd numbers

Posted 20 November 2011 - 07:08 PM

Why are you looking at firstnum in the loop? When you're in the loop, you no longer need to care about firstnum.

Do it by hand: list the odd numbers between 14 and 20. How do you go about it? Once you've got to 15, do you care about 14 any more?

We're talking about the numbers between 14 and 20, so the first number you care about is 15 (firstnum+1). Is that odd? If not, the next number will be. Now you know what the first number you're going to look at will be. And you know that the only numbers you're going to look at will be found by adding two to it.

Now you start your loop. In the loop, you check if the number you're looking at is less than the ending number - you already know that it's greater than firstnum and that it's odd, because you've arranged for that. So if it's less than your upper bound, you collect it and that's the end of the loop.

Does that make sense?
Was This Post Helpful? 1
  • +
  • -

#12 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 07:25 PM

yes it makes sense somewhat but i dont understand how to keep my counter from going over my second number with my second number being 10 its adding 2 to the 9 because its less than my second number making it 11.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,099
  • Joined: 19-March 11

Re: While loops odd numbers

Posted 20 November 2011 - 07:28 PM

Well, you look at the number before you collect it.

Suppose I know I have an odd number in my hand. I look at it, and if it's over the limit, I know I'm done, so I stop. If it's under the limit, then I collect it, then I add two, then I do it all again.
Was This Post Helpful? 1
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: While loops odd numbers

Posted 20 November 2011 - 07:33 PM

You have the loop condition, use it to make sure counter wont exceeds the limit.
Was This Post Helpful? 1
  • +
  • -

#15 BaumbachR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-November 11

Re: While loops odd numbers

Posted 20 November 2011 - 07:47 PM

View Postjon.kiparsky, on 20 November 2011 - 07:28 PM, said:

Well, you look at the number before you collect it.

Suppose I know I have an odd number in my hand. I look at it, and if it's over the limit, I know I'm done, so I stop. If it's under the limit, then I collect it, then I add two, then I do it all again.

Ok i know i have an odd by my statement but i dont know how to keep it from exceeding my maximum number...

View Postsmohd, on 20 November 2011 - 07:33 PM, said:

You have the loop condition, use it to make sure counter wont exceeds the limit.

ok i can do that with a for loop but idk how to with a while loop which is what i have to do.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2