Advertisement
mmayoub

arrays-exercise 09 slide 29

Jul 1st, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays09 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 9, slide 29
  10.          * input : 5 chars to an array
  11.          * output: are the values in the array is the same character
  12.          */
  13.        
  14.         final int ARRAY_SIZE = 5;
  15.         // create a scanner
  16.         Scanner s = new Scanner(System.in);
  17.  
  18.         // define an integer array
  19.         char[] chars = new char[ARRAY_SIZE];
  20.  
  21.         System.out.printf("Enter %d chars:\n", chars.length);
  22.         // get data from user and save it in the array
  23.         for (int i = 0; i < chars.length; i += 1) {
  24.             chars[i] = s.next().charAt(0);
  25.         }
  26.         s.close();
  27.  
  28.         // scan the array
  29.         int i;
  30.         for (i = 1; i < chars.length && chars[i] == chars[0]; i += 1) {
  31.  
  32.         }
  33.  
  34.         // i==chars.length --> all charactars are the same
  35.         System.out.printf("All characters are %sthe same\n",
  36.                 i == chars.length ? "" : "not ");
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement