944,124 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2666
  • Java RSS
Apr 19th, 2007
0

error checking

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Apr 19th, 2007
0

Re: error checking

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
May 2nd, 2007
0

Re: error checking

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
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: error messages
Next Thread in Java Forum Timeline: Looping through an Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC