Thread: Binary search

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Binary search


    Code:
    import java.lang.*;
    import java.util.Arrays;
    import java.util.Scanner;
    public class NinjaName
    {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            String[] equi = {"ka","zu","mi","te","ku","lu","ji","ri","ki",
                            "zul","me","ta","rin", "to","mo","no","ke","shi",
                            "ari","chi","do","ru","mei","na","fu","zi"};
            String[] list = new String[26];
            for(int counter=0; counter<equi.length;counter++)
            {
                char ch ='a';
                
                String temp = (ch+=counter)+"";
                list[counter]=temp;
            }
            
            System.out.print("ENTER YOUR NAME: ");
            String name = scan.next();
            
            int count = 0;
            while(count<name.length())
            {
                char chars = name.charAt(count);
                int index = Arrays.binarySearch(list, chars);
                count++;
            }
        }
    }
    java.lang.ClassCastException:
    java.lang.Character cannot be cast to java.lang.String(in java.lang.String)

    i dont understand that error! please help
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    java.lang.ClassCastException:
    java.lang.Character cannot be cast to java.lang.String(in java.lang.String)
    You left off the part of the error message that showed the statement that the error is on.

    an instance of the class: Character can not be cast to a String.
    You can create a String from characters by using one of the String class's constructors.
    Also the Character class has a method to create a String from a char.
    Last edited by NormR; December 5th, 2013 at 09:30 AM.

IMN logo majestic logo threadwatch logo seochat tools logo