Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 8:48 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Invalid integer formats
PostPosted: Tue Jul 12, 2011 8:48 pm 

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

}
}
[/code]


TOP
  
Reply with quote  
 Question subject: Re: Invalid integer formats
PostPosted: Wed Jul 13, 2011 1:24 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
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);
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 convert integer number to octal,hexadecimal number systems
 invalid argument on IE with script to center objects
 Ensuring integer are written as 32 bits
 rounding performed under integer division
 handle integer overflows and underflows
 Dividing two Integer values
 Integer value compare with equal sign
 finding the largest integer
 integer value overflow
 Invalid unicode sequence error ("u")

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: Yahoo [Bot] and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team