【问题标题】:Java: How do i get the int that i need??? by using something to split them?Java:我如何获得我需要的 int ???通过使用某些东西来拆分它们?
【发布时间】:2014-02-08 05:05:24
【问题描述】:
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);
}

我想知道是否有办法提取我需要的信息并将它们解析为整数???类似于拆分方法??

【问题讨论】:

  • 形状的字符串——它来自哪里——真的是那样吗,它是 XML 的一部分吗?你有什么东西可以正式定义它是什么吗?
  • 我要手动输入
  • 但我还没到那里.. 我要做的是输入格式 x y x y
  • 然后我需要一些函数来提取我输入的内容并将它们放入变量中并绘制形状
  • 像 XML 但不是 XML?

标签: java string split int shape


【解决方案1】:

您可以使用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();
    }
}

执行上述代码后,您可以使用整数列表中的值设置形状坐标。

【讨论】:

    【解决方案2】:

    您可以使用String#split() 将字符串拆分为数组字符串 然后,解析这个数组的一些元素。

    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);
        }
    

    【讨论】:

      【解决方案3】:

      使用 Integer.parseInt 但在 this.x1 之前也没有 int

      http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

      还可以在您将接近的空间中使用 shape.split。

      你可以从 shape.substring 和 shape.indexOf 开始获取带有数字的字符串。

      我不想给你代码,但你可能会觉得这很有帮助。

      http://codingbat.com/doc/java-string-indexof-parsing.html

      【讨论】:

        【解决方案4】:

        看起来您想要解析 XML,然后解析文本节点的内容。

        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]);
        

        【讨论】:

        • 计算机是愚蠢的,你需要自己想办法向他们解释你需要他们做什么,如果你不这样做,他们就做不到。现在你似乎不知道你想让计算机做什么——我建议回到你的老师那里,让他们澄清要求——这就是所有编程的工作方式,你一直需要弄清楚到底是什么人们想要/意义。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-14
        • 1970-01-01
        • 2019-05-27
        • 1970-01-01
        • 2016-10-08
        • 2013-10-24
        • 1970-01-01
        相关资源
        最近更新 更多