Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. package integerarrayexceptions;
  2. import java.util.Scanner;
  3. import java.util.InputMismatchException;
  4.  
  5. public class IntegerArrayExceptions {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner input = new Scanner(System.in);
  10.  
  11. int numbers[] = new int[10];
  12.  
  13. try{
  14.  
  15. System.out.print("Please enter 10 integers to store in an array and then press enter: ");
  16.  
  17. for(int index = 0; index < numbers.length; index++)
  18. numbers[index] = input.nextInt();
  19.  
  20. if(numbers.length==10){ //method doesnt work properly if you input over 10 integers, only if you input less
  21. System.out.print("Thanks for entering 10 integers. Now input an integer to check: ");
  22. int compare = input.nextInt();
  23.  
  24. if(numbers[index] == compare){
  25. System.out.print(compare);
  26. }
  27.  
  28. }
  29. }
  30. catch (InputMismatchException MismatchException)
  31. {
  32. System.out.print("One or more values entered is not an integer.");
  33. }
  34.  
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement