1 Replies - 630 Views - Last Post: 28 January 2011 - 10:03 PM Rate Topic: -----

#1 gracie891  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 11

Extract odd numbers and verify there is an odd number of them

Posted 28 January 2011 - 07:48 PM

Hi
I have written the below code and I am supposed to do several things, one is ensure its a 6 digit number, which seems to work. Second is where there are errors, I need to determine there are an odd number of odd numbers:

I keep getting that the else statement needs an if..which it seems to have and either exit code 1 or 2 depending on how I manipulate things.

import javax.swing.JOptionPane;		//Allows for JOptionPane to be initiated


    public class password {

     public static void main(String[] args) {

		int passInput;		//The user's password
		String inputString;

		//Get the password from user


		inputString =
			JOptionPane.showInputDialog("What is your password");

passInput = Integer.parseInt(inputString);

 		if (inputString.length()== 6)
{
	System.out.println("Password is valid.");
	System.out.println("Input:"+inputString);
	System.out.println("Password is invalid");
	System.out.println("End of Processing.");

}
else if(inputString =(int) n%10);

	if (inputString %2 == 1)  // if digit is odd
{
                result += digit * Math.pow(10,power);
                power++;
            }
            n/=10;

            {
	System.out.println("Password is valid.");
	System.out.println("Input:"+inputString);
	System.out.println("Password is invalid");
	System.out.println("End of Processing.");
}
	else
{
	System.out.println("Password Checker");
	System.out.println("Input:"+inputString);
	System.out.println("Password is invalid");
	System.out.println("End of Processing.");

}


		System.exit(0);
	}
}


Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Extract odd numbers and verify there is an odd number of them

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12176
  • View blog
  • Posts: 45,242
  • Joined: 27-December 08

Re: Extract odd numbers and verify there is an odd number of them

Posted 28 January 2011 - 10:03 PM

First, please properly indent your code so we can read it. Your logic errors should become more apparent.

Second, see your else if statement else if(inputString =(int) n%10);. You end it with a semi-colon, and compare the inputString to an int using the = operator. First, Strings should always be compared to other Strings using the equals() method, not the == operator.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1