error checking

Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

error checking

 
0
  #1
Apr 19th, 2007
I would like to make so that when you have a int and prompting user to enter some number for calculations... if the number entered is larger than a 10digits, my program wouldn't just crash or if the user enters a string... I havn't be taught any error catching stuff yet but it would be good to have my simple command line program to run without crashing...
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: error checking

 
0
  #2
Apr 19th, 2007
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3. public class test
  4. {
  5. public static void main(String args[])
  6. {
  7. int a[] = new int[10];
  8. int counter = 0;
  9. Scanner scanner = new Scanner(System.in);
  10. boolean continueloop = true;
  11. do
  12. {
  13. try
  14. {
  15. for (int i = 0; i < 10; i++)
  16. {
  17. System.out.println("Enter an integer (-ve to quit): ");
  18. int num = scanner.nextInt();
  19. if (num < 0 || i == 9)
  20. {
  21. System.out.println("\nThank you for trying");
  22. continueloop = false;
  23. break;
  24. }
  25. else
  26. {
  27. a[i] = num;
  28. counter++;
  29. }
  30. }//end for
  31. }//end try
  32. catch (InputMismatchException s)
  33. {
  34. System.out.println("Invalid");
  35. scanner.nextLine();
  36. }
  37. }//end do
  38. while (continueloop);
  39. //test view array
  40. for (int i = 0; i < 10; i++)
  41. System.out.print(a[i] + ", ");
  42. }//end main
  43. }//end class

I've been reading the api and a tutorial, I don't understand how to use what's described in the api and my loop isn't working correctly for the other tutorial...

Bascially I want to enter 10 number but with some sort of error checking and keep prompting to enter the 10 number if any of them is invalid... I havn't done, throws and try catch in my study so I'm not even sure I've used them right.

http://java.sun.com/j2se/1.5.0/docs/...Exception.html
http://www.deitel.com/articles/java_...ons/index.html
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: error checking

 
0
  #3
May 2nd, 2007
using try and catch is an easy, but on the other hand definitive way to catch your error. your program will not crash, but if you make a wrong input, your program won't ask for any more.

for instance:

int numbers[] = new int[5];

try{
for(int count = 0; count < 5; count++){
numbers[count] = readNumber();
}
}
catch(Excetion ex){
// this is the part that would happen if an invalid value (maybe a
// String Object) was entered in case of a number.
}

everything behind the catch clause is runned both when an error occured, and when no error occured. if you want now to add all the numbers to one huge sum, you must remember that there is a possibility that not all the elements of the array contain the values you want them to contain
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC