Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class arrays06 {
- public static void main(String[] args) {
- /*
- * Exercise 6, slide 29
- * input : 10 chars to an array, and then one char more
- * output: number of appearances of the additional char in the array
- */
- // size of the array
- final int ARRAY_SIZE=10;
- // create a scanner
- Scanner s = new Scanner(System.in);
- // define an integer array
- char[] chars = new char[ARRAY_SIZE];
- System.out.printf("Enter %d chars:\n", chars.length);
- // get data from user and save it in the array
- for (int i = 0; i < chars.length; i += 1) {
- chars[i] = s.next().charAt(0);
- }
- System.out.println("enter a char to search for:");
- char chToFind = s.next().charAt(0);
- s.close();
- // check numbers and print event numbers only
- for (int i = 0; i < chars.length; i += 1) {
- // if it's the required character
- if (chars[i] == chToFind)
- // print it
- System.out.printf("char '%c' found at position %d\n", chars[i],
- i);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement