954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic java help with looping

So I'm trying to define a counter in a while loop, and print it from the class:

public class odd
{
	public static void main(String[] args)
	{
		System.out.println("Input your number");
		int x = IO.readInt();
		
		int odd = 0;
		int even = 0;
		
		while (x != 0)
		{
			if (x % 2 !=0)
			{
				odd = odd++;
			}
			else
			{
				even = even++;
			}
			x = IO.readInt();
		}
		System.out.println(even + "evens" + odd + "odds");
	}
}


Anyone know how I can print the variableseven and odd?

This program is attempting to take a continuous input of numbers from a user, and output the number of odds and evens:

We wish to develop a program that will count the number of even and odd integers in a set ("even" meaning divisible by 2, "odd" meaning not divisible by 2). We will use zero as an indicator that the set has been completely entered, and this zero should not be counted as part of the set. Ask the user for a sequence of integers, terminated by zero. Output the number even integers and the number of odd integers.

***IO is a module that we were provided with to read inputs and return errors***

ilikepaste
Newbie Poster
7 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
import java.util.ArrayList;
import java.util.Collections;

public class odd {

    public static void main(String[] args) {
        int x = 20;
        ArrayList<Integer> odd = new ArrayList<Integer>();
        ArrayList<Integer> even = new ArrayList<Integer>();
        while (x > 0) {
            if (x % 2 != 0) {
                odd.add(x);
            } else {
                even.add(x);
            }
            x--;
        }
        Collections.sort(odd);
        Collections.sort(even);
        for (int i = 0; i < odd.size(); i++) {
            System.out.println(odd.get(i));
        }
        for (int i = 0; i < even.size(); i++) {
            System.out.println(even.get(i));
        }
        System.out.println(even + "evens" + odd + "odds");
    }

    private odd() {
    }
}


but if you don't understood,

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

just stupid questions,

how is validated Numeric value from input

what's int x = IO.readInt();

time to go sleep

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

no need for odd = odd++ or even = even++
here's the code with some few changes:
import java.util.Scanner;

public class odd
      {
    	  public static void main(String[] args)
      {
      System.out.println("Input your number");
      Scanner reader = new Scanner(System.in);
      int x = reader.nextInt();
      int odd = 0;
      int even = 0;
      while (x != 0)
      {
      if (x % 2 !=0)
      {
       odd++;
      }
      else
      {
       even++;
      }
      x = reader.nextInt();
      }
      System.out.println(even + " evens " + odd + " odds");
      }
      }
terexberd
Newbie Poster
14 posts since Feb 2011
Reputation Points: 11
Solved Threads: 2
 

Thank you both for your help

ilikepaste
Newbie Poster
7 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: