[英]for loop (remember where last to look) & Character.isDigit()
I have to do a project about the periodic table of elements. 我必须做一个关于元素周期表的项目。 My dilemma is as follow.
我的困境如下。 The user send you a molecular equation.
用户向您发送一个分子方程式。 It can be of any length.
它可以是任何长度。 In this array there is Uppercase letters, Lowercase letters and numbers.
在此数组中,有大写字母,小写字母和数字。
I also have an array of objects. 我也有一系列对象。 Each object represents a element on the periodic table.
每个对象代表元素周期表上的一个元素。 Now I need to break this string the user send me up in smaller pieces - in pieces that is recognized by the array of objects.
现在,我需要打破此字符串,用户将其细分为一些较小的部分,这些部分可以被对象数组识别。 I also have to multiply the answer by either one or by the number right next to the last letter of the element.
我还必须将答案乘以一个或乘以元素最后一个字母旁边的数字。
I already tried the following. 我已经尝试了以下方法。 Make the string a char array.
将字符串设为char数组。 Run through the array from backwards - test for number (
Character.isDigit(a[i]);
), test for uppercase and lowercase... I always end up with the same problem. 从后向遍历数组-测试数字(
Character.isDigit(a[i]);
),测试大写和小写...我总是Character.isDigit(a[i]);
相同的问题。 I have no idea how long the string will be. 我不知道字符串会持续多久。 Lets say I find a number.
可以说我找到一个数字。 Then if the previous char is an lowercase letter I need to do another check backwards for the uppercase letter.
然后,如果前一个字符是小写字母,我需要向后检查另一个大写字母。 Then let's say I have the molecular equation and the amount I need to multiply it by - how do I let the computer know to start looking from the last uppercase letter.
然后说我有分子方程式和乘以它的数量-如何让计算机知道从最后一个大写字母开始查找。
I hope someone understand this! 希望有人能理解!
Really in need of some help. 确实需要一些帮助。
Another question: why come doesn't this code work: 另一个问题:为什么这段代码行不通:
String moleq = "HeKiLH2B6";
char[] a = moleq.toCharArray();
int g = moleq.length()-1;
int x = 1; //if not more than one of element - multiply getWeight by 1
int[][] indexofdigit = new int[moleq.length()][2];
int[] indexoflower = new int[moleq.length()];
for (int i = g; i <= 0; i--){
if (Character.isDigit(a[i])) {
String z = Character.toString(a[i]);
x = Integer.parseInt(z);
System.out.println("YES");
}
}
This code never print me a Yes? 此代码永远不会打印出我的意思?
Your loop never executes as you are looking for i <= 0 condition. 寻找i <= 0条件时,循环永远不会执行。 And it is not true at first, as g>0, if you change as i >= 0 it will work
起初是不正确的,因为g> 0,如果将其更改为i> = 0,它将起作用
For chemical formulas, maybe it would be interesting a regular expression (regexp). 对于化学式,正则表达式(regexp)可能会很有趣。 Something like ([AZ][az]?[0-9] )+.
像([AZ] [az]?[0-9] )+之类的东西。 If I am not wrong( ) , this isolates an element and its cardinality (for example Fe2O3 --> group 1 (Fe2) + group 2 (O3)).
如果我没记错( ),则会隔离一个元素及其基数(例如Fe2O3->第1组(Fe2)+第2组(O3))。 Look at Pattern and Matcher for this.
查看模式和匹配器。
Note that with Java 5+ you can make that loop easier, using the for each loop: 请注意,在Java 5+中,可以使用for每个循环来简化该循环:
for (char c : a){
if (Character.isDigit(c)) {
String z = Character.toString(c);
x = Integer.parseInt(z);
System.out.println("YES");
}
}
To expand SJuan's suggestion: 扩展SJuan的建议:
String input = "HeKiLH2B6";
Pattern p = Pattern.compile("([A-Z][a-z]*)(\\d*)");
Matcher m = p.matcher( input );
while(m.find()){
String element = m.group( 1 );
String cardinalityStr = m.group( 2 );
int cardinality= 1;
if( cardinalityStr != null && cardinalityStr .length() > 0)
{
cardinality= Integer.parseInt( cardinalityStr );
}
System.out.println( element + cardinality);
}
Yields: 产量:
He1
Ki1
L1
H2
B6
Edit: this would also work if the user entered something like He-Ki-L-H2-B6
or He Ki L H2 B6
. 编辑:如果用户输入了
He-Ki-L-H2-B6
或He Ki L H2 B6
类的内容,这也将起作用。
The answer of SJuan76 in code (with the reluctant group qualifier added): 代码中SJuan76的答案(添加了勉强的组限定符):
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
// the compiled pattern can be re-used, are thread-safe
// and thus can be static final
Pattern p = Pattern.compile("([A-Z][a-z]?[0-9]*)+?");
// Per molecule (well, string) a matcher must be obtained.
Matcher m = p.matcher("HeKiLH2B6");
while(m.find()) {
System.out.println(m.group());
}
}
}
This prints: 打印:
He
Ki
L
H2
B6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.