mmayoub

arrays-exercise 02, slide 28

Jul 1st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays02 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 2, slide 28
  10.          * input : 10 chars to an array
  11.          * output: the keys of the upper case letters
  12.          */
  13.         // create a scanner
  14.         Scanner s = new Scanner(System.in);
  15.  
  16.         // define an integer array, size=10
  17.         char[] chars = new char[10];
  18.  
  19.         System.out.printf("Enter %d chars:\n", chars.length);
  20.         // get data from user and save it in the array
  21.         for (int i = 0; i < chars.length; i += 1) {
  22.             chars[i] = s.next().charAt(0);
  23.         }
  24.         s.close();
  25.  
  26.         // scan the chars atrray
  27.         for (int i = 0; i < chars.length; i += 1) {
  28.             // if upper case
  29.             if (chars[i] >= 'A' && chars[i] <= 'Z') {
  30.                 // print character
  31.                 System.out.printf("Upercase letter (%c) at position %d\n",
  32.                         chars[i], i);
  33.             }
  34.             System.out.println();
  35.         }
  36.     }
  37.  
  38. }
Add Comment
Please, Sign In to add comment