简体   繁体   English

Java-如何将Char []数组与字符串进行比较?

[英]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]; 我似乎在它声明的行上有问题: String [] oneRow =字母[i];

package MemoryGame;

import java.util.Scanner; 导入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: Eclipse说:

Type mismatch: cannot convert from char to String[] 类型不匹配:无法从char转换为String []

Thanks for your help! 谢谢你的帮助!

M 中号

You can't assign a single character to a String array. 您不能将单个字符分配给String数组。 You couldn't assign a single String to an array, either; 您也无法将单个String分配给数组; that part has nothing to do with chars. 这部分与字符无关。

Of course, you can't assign a char to a String directly, either, so you probably want something like this: 当然,您也不能直接将char分配给String,所以您可能想要这样的东西:

String onerow = String.valueOf(alphabet[i]);

if(oneRow.equals(letter))

The assignment of a character to a String is invalid. 字符分配给String无效。 You can use a concatenation trick to do it anyway, but that borders on a hack: 无论如何,您都可以使用串联技巧来做到这一点,但是这很容易被黑客入侵:

String[] oneLetter = ""+alphabet[i];

A better approach would be to check that letter has the length of 1 , and that its only character matches alphabet[i] : 更好的方法是检查letter的长度为1 ,并且其唯一字符与alphabet[i]相匹配:

if (letter.length() == 1 && letter.charAt(0) == alphabet[i]) {
    matchedLetter = alphabet[i];
    break;
}

char is incompatible with String and so String[] is not compatible with char[] . charString不兼容,因此String[]char[]不兼容。

alphabet[i] will return a char , you can transform it into a String by constructing a new one, for example: alphabet[i]将返回一个char ,您可以通过构造一个新的char将其转换为String ,例如:

char c = aplhabet[i];
String oneRow = String.valueOf(c);
if (oneRow.equals(letter)) { ... }

Or, you can compare the first char of the input String with the char you took from alphabet : 或者,你可以比较的第一个char输入的Stringchar你把alphabet

char c = aplhabet[i];
if (letter.chartAt(0) == c) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM