Reputation: 1035
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)
Upvotes: 3
Views: 2448
Reputation: 3748
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.
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)));
Upvotes: 1
Reputation: 5440
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 asString A
Upvotes: 4