Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* ==================================================
- Chapter 2: Syntax section C- Arrays
- Ex9: identical char array
- ===================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- final int SIZE=5;
- char arr[]=new char[SIZE];
- Scanner s=new Scanner(System.in);
- char c;
- boolean isIdentical=true;
- //init array with char
- System.out.println("Enter 5 characters: ");
- for(int i=0;i<arr.length;i++){
- arr[i]=s.next().charAt(0);
- }
- //get one of the char to compare with others
- c=arr[0];
- //loop until found different chars
- for(int i=1;i<arr.length;i++){
- if(c!=arr[i]) {
- System.out.println("Not identical!");
- isIdentical=false;
- break;
- }
- }
- //print only if all array values identical
- if(isIdentical){
- System.out.println("identical letters!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment