Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public class ParallelArray {
  2.  
  3. public static Scanner sc = new Scanner(System.in);
  4.  
  5. public static void main(String[] args) {
  6. char[] charArray = new char[5];
  7. int[] intArray = new int[5];
  8. char ch;
  9. int count;
  10. System.out.println("Enter 5 characters: ");
  11. for (int i = 0; i < charArray.length; i++) {
  12. charArray[i] = sc.next().charAt(0);
  13. }
  14.  
  15. do {
  16. System.out.println("Enter a character: ");
  17. ch = sc.next().charAt(0);
  18. int location = search(ch, charArray);
  19. intArray[location]++;
  20. System.out.println("Again? 1-yes, 0-no");
  21. count = sc.nextInt();
  22. } while (count == 1);
  23.  
  24. printBothLists(charArray, intArray);
  25. }
  26.  
  27. public static void printBothLists(char[] charArray, int[] intArray) {
  28. for (int i = 0; i < charArray.length; i++) {
  29. System.out.println(charArray[i] + " - " + intArray[i]);
  30. }
  31. }
  32.  
  33. public static int search(char ch, char[] charArray) {
  34. int count = -1;
  35. for (int i = 0; i < charArray.length; i++) {
  36. if (ch == charArray[i]) {
  37. count = i;
  38. return count;
  39. }
  40. }
  41. return count;
  42. }
  43. }
  44.  
  45. int location = search(ch, charArray);
  46. intArray[location]++; // <-- The index is invalid...
  47.  
  48. int location = search(ch, charArray);
  49. if (location >= 0 && location < intArray.length) {
  50. intArray[location]++; // <-- The index is invalid...
  51. } else {
  52. System.out.println("'" + ch + "' does not exist");
  53. }
Add Comment
Please, Sign In to add comment