为什么“ C”!=“ C”?

问题描述 投票:0回答:1

现在我正在写我的小型摩尔质量计算器,我无法修复一个错误。在MainActivity.kt中,我从.xml文件填充数组,然后使用Regex解析用户输入。但是,如果我在程序中键入“ C”(碳),则无法识别。为什么?

MainActivity.kt:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val adapter = MoleculeAdapter(this)
    moleculeView.layoutManager = LinearLayoutManager(this)
    moleculeView.setHasFixedSize(true)
    moleculeView.adapter = adapter

    val parser = resources.getXml(R.xml.elements)
    val elements = Array(127) { Element() }
    thread {
        var i = 0
        while (parser.eventType != END_DOCUMENT) {
            if (parser.eventType == START_TAG && parser.name == "element") {
                elements[i].number = parser.getAttributeIntValue(null, "number", 0)
                elements[i].letter = parser.getAttributeValue(null, "letter")
                elements[i].name = parser.getAttributeValue(null, "name")
                val weight = parser.getAttributeValue(null, "weight")
                elements[i].weight = if (weight.isNotEmpty()) weight.toFloat() else 0F
                i++
            }
            parser.next()
        }
        parser.close()
    }.join()

    Log.i("elements:", elements.joinToString { it.toString() + "\n" })

    val lowerCaseLetters = "abcdefghiklmnopqrstuy"
    val elementsRegex = Regex("""[ABCDEFGHIKLMNOPRSTUVWXYZ]([$lowerCaseLetters]{2}|[$lowerCaseLetters]?)\d*""")
    val digitsRegex = Regex("""\d+""")
    formulaInput.doOnTextChanged { text, _, _, _ ->

        lateinit var foundedElements: List<Element>
        thread {
            foundedElements = elementsRegex
                .findAll(text ?: "")
                .map {
                    elements.find { element ->
                        Log.i("value", it.value + " " + it.value)
                        if (it.value.filter { it.isLetter() } == element.letter) {

                            val number = digitsRegex.find(it.value)
                            if (number != null) {
                                try {
                                    element.moleculeCount = number.value.toInt()
                                    element.weight = element.weight * number.value.toInt()
                                } catch (e: NumberFormatException) {  }
                            }
                            element.percentage = adapter.getTotalWeight(element.weight) * 100
                            true
                        } else false
                    }
                }.filterNotNull().toList()
        }.join()

        adapter.insertElements(foundedElements)
    }
}

}

Element.kt:

data class Element(var number: Int = -1,
               var letter: String = "",
               var name: String = "",
               var weight: Float = 0F,
               var percentage: Float = 100F,
               var moleculeCount: Int = 1)

xml文件项目示例:

    <element
    number="6"
    letter="С"
    name="Углерод"
    weight="12.011" />
android regex kotlin xml-parsing
1个回答
0
投票

我不敢相信,在我的xml文件中,字母“С”是西里尔字母“ C”(\ u0421)!并且由于此等于检查“С” ==“ C”失败。

非常感谢WiktorStribiżew的评论。

© www.soinside.com 2019 - 2024. All rights reserved.