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.

Recommended Answers

All 2 Replies

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/api/java/util/InputMismatchException.html
http://www.deitel.com/articles/java_tutorials/20060408/Arithmetic_InputMismatch_Exceptions/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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.