Results 1 to 5 of 5
  1. #1
    Dooley is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default A little help with my program?

    In short, what I'm making here is a little program that when you enter a number in the input, it decides whether or not it is even or odd. Here it is:

    Java Code:
    import java.io* ;
    
    public class OddEven
    {
    	public static void main( String args[] )
    	{
    		BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)) ;
    		String inputData ;
    		try {
    			String inputData = inputReader.readLine();
    		}
    		catch(IOException e) {
    			System.out.println("Error reading keyboard input") ;
    		
    		float f ;
    try{
    f = Float.parseFloat(inputData) ;
    }
    catch(NumberFormatException e) {
    System.out.println("Invalid value. Did you enter a number?") ;
    }
    
    
    		
    		}
    		{
    			if (inputData % 2 == 0) 
    				System.out.println( "Even" );
    			else if (inputData % 2 != 0)
    				System.out.println( "Odd" );
    		}
    	}
    
    				
    }

    When I try to compile it in the Terminal, there is an error

    Java Code:
    OddEven.java:2: ';' expected
    import java.io* ;
                  ^
    1 error
    Could someone help me out here and tell me what to change? Keep in mind that I am very new to programming (literally started yesterday)

  2. #2
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Looks like your missing some brackets around the try catches and the ifs.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Brackets may be alright, but your indentation is god-awful so it's very hard to tell. It is to your benefit to try to standardize your indentation.

    Regarding your problem, you're missing a period:

    Java Code:
    import java.io.*;

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    It looks like you're way over-complicating this. Why are you parsing a float?

    Java Code:
    public static void main(String[] args) {
    		int number = 0;
    		Scanner s = new Scanner(System.in);
    		while(true) {
    			try {
    				System.out.println("Please enter a valid number!");
    				number = Integer.parseInt(s.next());
    				break;
    			} catch(NumberFormatException e) {
    				continue;
    			}
    		}
    		if(number % 2 == 0)
    		System.out.println("Even");
    		else
    		System.out.println("Odd");
    	}
    The above code does exactly what you're trying to do but with half the lines.
    Last edited by Solarsonic; 05-09-2011 at 01:54 AM.

  5. #5
    BZwap's Avatar
    BZwap is offline Member
    Join Date
    May 2011
    Location
    Lucknow
    Posts
    8
    Rep Power
    0

    Smile

    First of all remove "{" from this code-
    catch(IOException e) {
    System.out.println("Error reading keyboard input") ;
    __________________________________________________ ______
    Then remove a "{" from here-
    System.out.println("Invalid value. Did you enter a number?") ;
    }
    __________________________________________________ ______
    I hope that this will work

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •