Java:如何获得所需的int???用什么东西把他们分开? intx1; int y1; int-x2; int y2; String shape=“sequenceNumber 50 60 70 90” 公共拆分它们(字符串s){ //我不知道如何提取我需要的信息。。。 //…执行某些操作以提取它们(形状) int this.x1=解析字符串(“50”); int this.y1=解析字符串(“60”); int this.x2=解析字符串(“70”); int this.y2=解析字符串(“90”); } 公共空间图形(图形g){ g、 drawRect(x1,y1,x2,y2); }
我想知道是否有一种方法可以提取我需要的信息并将它们解析为整数???类似split方法的东西???使用Integer.parseInt,但在此之前也没有int.x1 也可以使用shape.split在一个你会接近的空间中 可以从shape.substring和shape.indexOf开始,只获取带有数字的字符串 我不想给你代码,但你可能会发现这很有帮助Java:如何获得所需的int???用什么东西把他们分开? intx1; int y1; int-x2; int y2; String shape=“sequenceNumber 50 60 70 90” 公共拆分它们(字符串s){ //我不知道如何提取我需要的信息。。。 //…执行某些操作以提取它们(形状) int this.x1=解析字符串(“50”); int this.y1=解析字符串(“60”); int this.x2=解析字符串(“70”); int this.y2=解析字符串(“90”); } 公共空间图形(图形g){ g、 drawRect(x1,y1,x2,y2); },java,string,split,int,shape,Java,String,Split,Int,Shape,我想知道是否有一种方法可以提取我需要的信息并将它们解析为整数???类似split方法的东西???使用Integer.parseInt,但在此之前也没有int.x1 也可以使用shape.split在一个你会接近的空间中 可以从shape.substring和shape.indexOf开始,只获取带有数字的字符串 我不想给你代码,但你可能会发现这很有帮助 您可以使用扫描仪扫描该字符串,并且使用nextInt()可以获取整数值 int x1; int y1; int x2; int y2; St
您可以使用
扫描仪扫描该字符串,并且使用nextInt()
可以获取整数值
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);
}
String s=“sequenceNumber 50 60 70 90”;
扫描仪=新的扫描仪;
列表=新的ArrayList();
while(scanner.hasNext()){
if(scanner.hasNextInt()){
添加(scanner.nextInt());
}
否则{
scanner.next();
}
}
执行上述代码后,您可以使用整数列表中的值设置形状坐标。看起来您需要解析XML,然后解析文本节点的内容
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();
}
}
String shape=“50 60 70 90”;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder DocumentBuilder=dbf.newDocumentBuilder();
documentdocument=documentBuilder.parse(新的InputSource(新的StringReader(shape));
Element square=document.getDocumentElement();
String textContent=square.getTextContent().trim();
字符串[]数字=textContent.split(“”);
int x1=整数.parseInt(数字[0]);
inty1=Integer.parseInt(数字[1]);
intx2=Integer.parseInt(数字[2]);
inty2=Integer.parseInt(数字[3]);
您可以使用
然后,解析这个数组的一些元素
String shape = "<Square> 50 60 70 90 </Square>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new StringReader(shape)));
Element square = document.getDocumentElement();
String textContent = square.getTextContent().trim();
String[] numbers = textContent.split(" ");
int x1 = Integer.parseInt(numbers[0]);
int y1 = Integer.parseInt(numbers[1]);
int x2 = Integer.parseInt(numbers[2]);
int y2 = Integer.parseInt(numbers[3]);
形状的字符串-它来自哪里-它真的是这样的吗,它是XML的一部分吗?你有正式定义它的东西吗?我必须手动输入它们,但我还没有。。我要做的是输入x y x y格式的东西,然后我需要一些函数来提取我输入的东西,并将它们放入变量中,然后画出形状,比如XML,但不是XML?计算机很笨,你需要自己找到一种方法,向他们解释你需要他们做什么,如果你不需要他们做不到的话。现在你似乎不知道你想让电脑做什么-我建议你回到你的老师那里,让他们澄清要求-这就是所有编程一直在工作的方式,你需要不断澄清人们到底想要什么/意思。
public void splitThem(String s){
// Split string to array
String[] array = s.split(" ");
System.out.println(Arrays.toString(array));
// Parse
this.x1 = Integer.parseInt(array[3]);
this.y1 = Integer.parseInt(array[4]);
this.x2 = Integer.parseInt(array[5]);
this.y2 = Integer.parseInt(array[6]);
System.out.println(x1);
System.out.println(y1);
System.out.println(x2);
System.out.println(y2);
}