Ray Marsh

Ranch Hand

Posts: 458

posted 17 years ago

Assignment 1.2 runs fine if an integer is entered, however, if letters or decimal values are entered I get this:java.lang.NumberFormatException: 1.5

at java.lang.Integer.parseInt(Compiled Code)

at java.lang.Integer.parseInt(Integer.java:458)

at EvenOdd.main(EvenOdd.java:4)

What to do? Here's my code.public class EvenOdd

{

public static void main(String[] args) {

// Convert String "args[0] to an int

int x = Integer.parseInt( args[0] );

// Perform modulo: divide by 2 and capture the remainder in int r

int r = x % 2;

// If the input is an even integer the remainder ( int r ) will be zero

if( r == 0 )

{

System.out.println("even");

}

// If the input is an odd integer the remainder will not be zero

else

{

System.out.println("odd");

}

}

} ///:~

Thanks.

at java.lang.Integer.parseInt(Compiled Code)

at java.lang.Integer.parseInt(Integer.java:458)

at EvenOdd.main(EvenOdd.java:4)

What to do? Here's my code.public class EvenOdd

{

public static void main(String[] args) {

// Convert String "args[0] to an int

int x = Integer.parseInt( args[0] );

// Perform modulo: divide by 2 and capture the remainder in int r

int r = x % 2;

// If the input is an even integer the remainder ( int r ) will be zero

if( r == 0 )

{

System.out.println("even");

}

// If the input is an odd integer the remainder will not be zero

else

{

System.out.println("odd");

}

}

} ///:~

Thanks.

Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – *Charles Spurgeon*