3

I would like to return the index of a user-input substring from a Character array using Java. The array is initialized, scrambled, then searched. I'm new to this and have tried two different approaches with no success. What am I overlooking? Thanks in advance.

Approach 1:

import java.lang.Math;
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;

public class ArrayRandomChar {
public static void main(String[] args) {
    // Create an array of characters:
    Character[] anArray = {'P', 'A', 'P', 'A', 'B', 'E', 'A', 'R'};
    for (char ch: anArray)
    System.out.print(ch + " ");
    System.out.println();

    // Create list from array and shuffle
    List<Character> aList = Arrays.asList(anArray);
    Collections.shuffle(aList);
    System.out.print(aList);
    System.out.println();

    // Bring back into array
    Object ob[] = aList.toArray();
    for (Object ch: anArray)
    System.out.print((Character) ch + " ");

    // User input
    Scanner input = new Scanner(System.in);
    String letter = input.next();
    System.out.println(letter);

    // Return index of letter, -1 if doesn't contain
    int retval = aList.IndexOf(letter);
    System.out.println(retval);

    }
      }

Issue: when running document and inputting a letter, -1 is returned even if the list contains the letter.

Approach 2:

public class ArrayRandomChar {
public static void main(String[] args) {

    // Create an array of characters:
    Character[] anArray = {'P', 'A', 'P', 'A', 'B', 'E', 'A', 'R'};
    for (char ch: anArray)
        System.out.print(ch + " ");
    System.out.println();

    // Create list from array:
    List<Character> aList = Arrays.asList(anArray);
    Collections.shuffle(aList);
    System.out.print(aList);
    System.out.println();

    //create array from list
    Object ob[] = aList.toArray();
    for (Object ch: anArray)
        System.out.print((Character) ch + " ");

    //user input
    Scanner input = new Scanner(System.in);
    String letter = input.next();
    int index = Arrays.binarySearch(anArray, letter);
    System.out.println(~index);
    System.out.println(letter);

    }
      }

This throws exception:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character
at java.lang.Character.compareTo(Character.java:101)
at java.util.Arrays.binarySearch0(Arrays.java:2001)
at java.util.Arrays.binarySearch(Arrays.java:1943)
at ArrayRandomChar.main(ArrayRandomChar.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
2
  • 1
    What don't you understand about the error message? Commented Mar 31, 2014 at 4:53
  • Everything beginning with 'at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)' I'm wondering if this could be solved with a Character compatible Scanner input.next(). Commented Mar 31, 2014 at 20:48

2 Answers 2

4

This will solve the issue..

Approach 1:

int retval = aList.indexOf(letter.charAt(0));

Approach 2:

int index = Arrays.binarySearch(anArray, letter.charAt(0));

The problem is that you are having a character Array aList, and the letter is a String and hence it search for a String in the Character array. Which can possibly cause ClassCastException or String not found.

In short : Character A is not same as String A

Sign up to request clarification or add additional context in comments.

Comments

1

Approach 1:

when running document and inputting a letter, -1 is returned even if the list contains the letter.

ok, this problems @Dileep has already answered,

int retval = aList.IndexOf(letter);

Mistake is that you have passed a String type to List.indexOf(Object o) were, aList contains all elements of type Character. So List.indexOf() will always return -1.

you may change to work:

int retval = aList.indexOf(Character.toUpperCase(letter.charAt(0)));

Note: Character.toUpperCase() i have used because you have all elements in upper case.


Approach 2:

throws

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character

exception message has already answered, that String cannot be a changed/cast to Character,

int index = Arrays.binarySearch(anArray, letter);

Here,

  • anArray contains elements type of Character

  • you have passed a String to Arrays.binarySearch(), instead of a char or Character, internally when Arrays#binarySearch() invokes Character.compareTo(T o) here, the specified object's type(i.e. letter#String) prevents it from being compared to Character object. then the ClassCastException is throwed.

Solution:

int index = Arrays.binarySearch(anArray, Character.toUpperCase(letter.charAt(0)));

2 Comments

I'm aiming for a randomized scramble of a pre-defined Character array. How would Arrays.sort() affect this?
@user25976 Sorry, i have not noticed that in your question. if you aiming for a randomized scramble, then no use of Arrays.sort() in that case. i will update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.