| | |
error checking
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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.
Thanks.
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
import java.util.Scanner; import java.util.InputMismatchException; public class test { public static void main(String args[]) { int a[] = new int[10]; int counter = 0; Scanner scanner = new Scanner(System.in); boolean continueloop = true; do { try { for (int i = 0; i < 10; i++) { System.out.println("Enter an integer (-ve to quit): "); int num = scanner.nextInt(); if (num < 0 || i == 9) { System.out.println("\nThank you for trying"); continueloop = false; break; } else { a[i] = num; counter++; } }//end for }//end try catch (InputMismatchException s) { System.out.println("Invalid"); scanner.nextLine(); } }//end do while (continueloop); //test view array for (int i = 0; i < 10; i++) System.out.print(a[i] + ", "); }//end main }//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
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
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
![]() |
Similar Threads
- Custom error checking (C#)
- User Form error checking (PHP)
- Help with 1.pointers and 2.error checking (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Basic Error Checking (C++)
- How to Perform Disk Error Checking in Windows XP (Windows tips 'n' tweaks)
Other Threads in the Java Forum
- Previous Thread: error messages
- Next Thread: Looping through an Array
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





