int x1;
int y1;
int x2;
int y2;
String shape = " sequenceNumber <Square> 50 60 70 90 </Square> "
public splitThem(String s){
//I can't figure out how to extract the information i need...
//... Do sometthing to extract splitThem(shape)
int this.x1 = parseString("50");
int this.y1 = parseString("60");
int this.x2 = parseString("70");
int this.y2 = parseString("90");
}
public void Graphics(graphic g){
g.drawRect(x1,y1,x2,y2);
}
我想知道是否有一种方法可以提取我需要的信息并将它们解析为整数???类似于 split 方法??
最佳答案
您可以使用Scanner
扫描该字符串,并使用nextInt()
您可以掌握整数值。
String s = " sequenceNumber <Square> 50 60 70 90 </Square> ";
Scanner scanner = new Scanner(s);
List<Integer> list = new ArrayList<Integer>();
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
else {
scanner.next();
}
}
执行上述代码后,您可以使用整数列表中的值设置形状坐标。
关于Java:我如何获得我需要的int???用什么东西来分裂它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642170/