Results 1 to 5 of 5
Thread: A little help with my program?
- 05-09-2011, 12:43 AM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
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
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)Java Code:OddEven.java:2: ';' expected import java.io* ; ^ 1 error
- 05-09-2011, 01:07 AM #2
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
Looks like your missing some brackets around the try catches and the ifs.
-
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.*;
- 05-09-2011, 01:27 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
It looks like you're way over-complicating this. Why are you parsing a float?
The above code does exactly what you're trying to do but with half the lines.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"); }Last edited by Solarsonic; 05-09-2011 at 01:54 AM.
- 05-09-2011, 10:07 AM #5
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
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks