Hello,
I am having trouble with my Odd/ Even Code. Can anyone help me with this? Or has a current code?

Dani AI

Generated

The original snippet contains a few classic, easy-to-overlook problems. The leading typo (mport instead of import), using number before assigning it (as noted), and the stray semicolon after the condition (if (number % 2 == 0);) are the immediate causes of compile/run errors and of the condition doing nothing. 's fix (parsing the input string and adding braces) correctly resolves those basic faults; the later posts from and show console-based alternatives and a pattern-printing extension.

For a more robust solution: parse input inside a safe check or try/catch so non-numeric input does not crash the program; always use braces with if/else to avoid logic mistakes caused by stray semicolons or misplaced statements; keep GUI (JOptionPane) and console I/O separate unless intentionally mixing them. For the pattern request from , compute the midpoint as (lines+1)/2, print ascending rows up to that midpoint, then descending rows — and validate the line count is odd and within the 1..11 limit before printing.

Example console-safe parity checker (Scanner + validation):

import java.util.Scanner;

public class EvenOddSafe {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        while (!sc.hasNextInt()) {
            sc.next(); // discard bad token
            System.out.print("Enter a valid integer: ");
        }
        int n = sc.nextInt();
        System.out.println(n + (isEven(n) ? " is EVEN" : " is ODD"));
    }

    static boolean isEven(int x) {
        return x % 2 == 0;
    }
}

This complements the GUI example from and the console examples already posted, while adding input validation and a small helper for readability.

Recommended Answers

All 7 Replies

where is your odd/even code ... let us have a look over it. Only then we'll be able to help you.

Hello,
I am having trouble with my Odd/ Even Code. Can anyone help me with this?

Here is what I have so far, but the program is not recognizing the (number) after if ( number). So if anyone can help I would sure appreciate it.

mport javax.swing.JOptionPane;  // program uses JOptionPane
public class EvenOdd {
/** Creates a new instance of EvenOdd */
public EvenOdd() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String firstNumber;    // first string entered by user
int number;           // number to be read
int even;            // number if even
int odd;             // number if odd
firstNumber = JOptionPane.showInputDialog( null, " Enter a number" );
if ( number%2 == 0 );
System.out.print( " Number is EVEN " );

Well, you are trying to use "number" in your if statement. You do not have number set to any value. Maybe you meant "firstNumber" instead of "number".That should get you going in the right direction

try it now

import javax.swing.JOptionPane; // program uses JOptionPane

public class EvenOdd {

   public static void main(String[] args) {
	  String firstNumber; 
	  int number; 

	  firstNumber = JOptionPane.showInputDialog( null, " Enter a number" );
	  number = Integer.parseInt(firstNumber);
	  
	  if ( number%2 == 0 ){
		 System.out.print( " Number is EVEN " );
	  }
	  else 
		 System.out.print("Number is ODD");
   }
}

Can any one help me Plz
- write an java program that outputs the followingpattern. Th program should prompt the user an osdd nmbr of lines (limit 1 to 11).
For example, if the user prompt the number of lines is 9, the the outputs should be :
+
++
+++
++++
+++++
++++
+++
++
+

import java.util.*;
import java.io.*;

public class kaze{
     public static void main(String[] args) throws IOException{
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      Integer number;
      System.out.println("Input A Number:"); 
      number = Integer.parseInt(in.readLine());


      if (number % 2 == 0)
      {
        System.out.println(" Even.");
      }
      else
      {
        System.out.println(" Odd.");
      }
    }
}
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.Number;

public class OddEven{

	public static void main(String[] args)throws Exception{
	
		BufferedReader bufRead = new BufferedReader(new InputStreamReader(System.in));

		String strNumber;
		int number=2;
		System.out.println("Enter a number:");
		strNumber = bufRead.readLine();
		number = Integer.parseInt(strNumber);


		if (number % 2 == 0)
		{
			System.out.println(" Even Number Entered is : "+number);

			for(int i=0; i < number ; i++)
			{
				for(int j=0 ; j< i ; j++)
				{
					System.out.print("+");
				}
			System.out.println("");
			}

			for(int i=number ; i >0 ; i--)
			{
				for(int j=i ; j >0 ; j--)
				{
					System.out.print("+");
				}
			System.out.println("");
			}

		}
		else
		{
			System.out.println(" Odd Number Entered is : "+number);
			for(int i=0; i < number ; i++)
			{
				for(int j=0 ; j< i ; j++)
				{
					System.out.print("+");
				}
			System.out.println("");
			}

			for(int i=number ; i >0 ; i--)
			{
				for(int j=i ; j >0 ; j--)
				{
					System.out.print("+");
				}
			System.out.println("");
			}

		}
	System.out.println("------------------Thank You --------------------------------");
	}
}

new_programmer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.