Tue Jul 12, 2011 8:48 pm
import java.util.*;
public class EH1 {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int k=1,c=0,sum=0,invalid=0,valid=0;
System.out.println("Enter 10 numbers :");
while (k<=10)
{
try
{
c = s.nextInt();
sum =sum + c;
valid++;
k++;
}
catch(NumberFormatException ex)
{
k++;
System.out.println(" INVALID NUMBER ");
invalid++;
}
}
System.out.println("Valid numbers : "+valid);
System.out.println("Invalid numbers : "+invalid);
System.out.println("SUM : "+sum);
}
}
Wed Jul 13, 2011 1:24 pm
Scanner s = new Scanner(System.in);
int k = 1, c = 0, sum = 0, invalid = 0, valid = 0;
String number="";
System.out.println("Enter 10 numbers :");
while (k <= 10) {
try {
number = s.next();
c=0;
c=Integer.parseInt(number);
sum = sum + c;
System.out.println("Sum so far"+sum);
valid++;
k++;
} catch (NumberFormatException ex) {
k++;
System.out.println(" INVALID NUMBER ");
invalid++;
}catch (InputMismatchException exception){
k++;
System.out.println(" INVALID NUMBER ");
invalid++;
}
}
System.out.println("Valid numbers : " + valid);
System.out.println("Invalid numbers : " + invalid);
System.out.println("SUM : " + sum);
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.