Java 如何使用StringTokenizer类
我应该通过使用StringTokenizer类将字符串拆分为标记来计算字符串。之后,我应该使用“Integer.parseInt”将这些标记转换为int值 我不明白的是,在分割代币之后,我应该如何处理它们Java 如何使用StringTokenizer类,java,stringtokenizer,Java,Stringtokenizer,我应该通过使用StringTokenizer类将字符串拆分为标记来计算字符串。之后,我应该使用“Integer.parseInt”将这些标记转换为int值 我不明白的是,在分割代币之后,我应该如何处理它们 public class Tester { public static void main(String[] args) { String i = ("2+5"); StringTokenizer st = new StringTokenizer(i, "+-", t
public class Tester {
public static void main(String[] args) {
String i = ("2+5");
StringTokenizer st = new StringTokenizer(i, "+-", true);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
int x = Integer.parseInt();
//what exactly do I have to type in here, do convert the token(s) to an int value?
}
}
如果我现在明白了,我现在有三个代币。这将是:“2”,“+”和“5”
如何准确地将这些标记转换为int值?
我必须分别转换它们吗
非常感谢您的帮助。也许您可以使用:
String i = ("2+5");
StringTokenizer st = new StringTokenizer(i, "+-", true);
while (st.hasMoreTokens()) {
String tok=st.nextToken();
System.out.println(tok);
//what exactly do I have to type in here, do convert the token(s) to an int value?
if ("+-".contains(tok)) {
//tok is an operand
}
else {
int x = Integer.parseInt(tok);
}
}
也许你可以用这个:
String i = ("2+5");
StringTokenizer st = new StringTokenizer(i, "+-", true);
while (st.hasMoreTokens()) {
String tok=st.nextToken();
System.out.println(tok);
//what exactly do I have to type in here, do convert the token(s) to an int value?
if ("+-".contains(tok)) {
//tok is an operand
}
else {
int x = Integer.parseInt(tok);
}
}
要使用从字符串中提取的整数进行一些计算,必须将它们放入ArrayList中。您必须使用try/catch操作来避免NumberFormatException。此外,您还可以直接从ArrayList中获取值,并根据需要使用它们。例如:
public static void main(String[] args) {
ArrayList <Integer> myArray = new ArrayList <>();
String i = ("2+5");
StringTokenizer st = new StringTokenizer(i, "+-/*=", true);
while (st.hasMoreTokens()) {
try {
Integer stg = Integer.parseInt(st.nextToken(i));
myArray.add(stg);
}
catch (NumberFormatException nfe) {};
}
System.out.println("This is an array of Integers: " + myArray);
for (int a : myArray) {
int x = a;
System.out.println("This is an Integer: " + x);
}
int b = myArray.get(0);
int c = myArray.get(1);
System.out.println("This is b: " + b);
System.out.println("This is c: " + c);
System.out.println("This is a sum of b + c: " + (b + c));
}
要使用从字符串中提取的整数进行一些计算,必须将它们放入ArrayList中。您必须使用try/catch操作来避免NumberFormatException。此外,您还可以直接从ArrayList中获取值,并根据需要使用它们。例如:
public static void main(String[] args) {
ArrayList <Integer> myArray = new ArrayList <>();
String i = ("2+5");
StringTokenizer st = new StringTokenizer(i, "+-/*=", true);
while (st.hasMoreTokens()) {
try {
Integer stg = Integer.parseInt(st.nextToken(i));
myArray.add(stg);
}
catch (NumberFormatException nfe) {};
}
System.out.println("This is an array of Integers: " + myArray);
for (int a : myArray) {
int x = a;
System.out.println("This is an Integer: " + x);
}
int b = myArray.get(0);
int c = myArray.get(1);
System.out.println("This is b: " + b);
System.out.println("This is c: " + c);
System.out.println("This is a sum of b + c: " + (b + c));
}
必须将从
st.nextToken()
返回的值存储在字符串变量中。而且,文档是你的朋友;应该对您有所帮助。现在您正在将令牌打印到标准输出,但没有将其保存到任何地方。考虑在数组中保存圣NEXTROKEN()的输出,然后您可以稍后访问它来执行整数。PSESETIN(),您必须在字符串变量中存储从<代码>圣NEXTROKEN()/<代码>返回的值。而且,文档是你的朋友;应该对您有所帮助。现在您正在将令牌打印到标准输出,但没有将其保存到任何地方。考虑在数组中保存圣NeXToTK()的输出,然后您可以稍后访问它来执行整数。