vendredi 13 mars 2015

Java - How to Compare Char[] Array with String?



I have a question regarding the comparison of the characters within an Array with a specific input which is a String.


I seem to have a problem at line where it states: String[] oneRow = alphabet[i];



package MemoryGame;


import java.util.Scanner;


public class Memory {



/*
* array list of the alphabet.
*/

char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

/*
* Constructor
*/
public Memory(){

/*
* Scanner initiation for input end-user.
*/
System.out.println("What is the first letter of you name? ");
Scanner scan = new Scanner(System.in);
String letter = scan.nextLine();
String s = scan.nextLine();

/*
* Comparing Input with array
*/
String[] matchedRow;
for(int i=0; i<alphabet.length; i++)
{
String[] oneRow = alphabet[i];

if(oneRow[0].equals(letter))
{
matchedRow = oneRow;
break;
}
}

for(int i=0; i<matchedRow.length; i++)
{
System.out.println(matchedRow[i]);

}
}

public static void main(String[] args) {
Memory memory = new Memory();
}


Eclipse says:



Type mismatch: cannot convert from char to String[]



Thanks for your help!


M




Aucun commentaire:

Enregistrer un commentaire