Java:我如何获得我需要的int???用什么东西来分​​裂它们?

标签 java string split int shapes

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/

相关文章:

java - 使用标签创建的分数字符串在分数动态变化时不会删除以前的分数-LibGdx

string - Flutter 拆分并使特定单词加粗

C#读取文件添加特殊字符

java - 我想从串行通信中分离数据并将其传递到表中

r - 根据特定标记从数据框中拆分字符列

java - 语句 lambda 可以替换为表达式 lambda

java - 访问外部资源

java - 在具有多个参数的 ArrayList 中搜索

java - 从键盘读取数据并存储在字符串数组中

java - 两个字符串相等,但不适用于 if 语句