Total members 11890 |It is currently Sat Apr 20, 2024 3:07 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





i want to find the sum of 10 numbers entered (valid) and also find the no. of valid and non valid nos. entered ....i made the program below but it doesn't work...and need modification in it ?


Code:

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);
        
    
}
}
 





Author:

see the modification i made below
Code:

 Scanner s 
= new Scanner(System.in);
        
int k 10sum 0invalid 0valid 0;
        
String number="";
        
System.out.println("Enter 10 numbers :");

        while (
<= 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);
 


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Invalid integer formats
 Invalid Argument error in IE7     -  
 invalid drive or directory     -  
 invalid argument on IE with script to center objects     -  
 integer value overflow     -  
 Dividing two Integer values     -  
 finding the largest integer     -  
 Integer value compare with equal sign     -  
 handle integer overflows and underflows     -  
 rounding performed under integer division     -  
 Ensuring integer are written as 32 bits     -  



Topic Tags

Java Variables
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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