Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将tostring输出拆分为字符串_Java - Fatal编程技术网

Java 将tostring输出拆分为字符串

Java 将tostring输出拆分为字符串,java,Java,这是我的figure类tostring方法超类 public String toString() { return "x=" + x + ", y=" + y + ", color=" + color ; } public String toString() { return "x-"+ x + "-y-" + y + "-color-" +color ; } 这是矩形类tostring方法扩展的图形类 @Override public String toString() {

这是我的figure类tostring方法超类

public String toString() {
    return "x=" + x + ", y=" + y + ", color=" + color ;
}
public String toString() {
    return "x-"+ x + "-y-" + y + "-color-" +color ;
}
这是矩形类tostring方法扩展的图形类

@Override
public String toString() {
    return "Rectangle [ "+  super.toString()   + ", width=" + width + ", length=" + length+  "]\n";
}
@Override
public String toString() {
    return super.toString()+"-width-" + width + "-length-"+length+ ";
}
我想通过拆分这个tostring方法来再次绘制一个矩形,从而只获取x、y、颜色、宽度和长度的值

我的整个程序必须通过点击2个点来绘制矩形。当我点击退出按钮时,它会将矩形对象保存在一个文本文件中。当我再次运行它时,它必须弹出我以前绘制的形状。我需要使用前面的点再次绘制矩形。
感谢您的帮助。

为了覆盖矩形类中的toString,您的Figure类必须是矩形的子类。不是像你这样的超级班级。使用以下命令:

public class Figure extends Rectangle { /* Other code here */ }
然后,需要在toString方法中使用矩形子类中的值。因为矩形类没有getColor方法,所以需要在自己的Figure类中创建它。应该是这样的:

@Override
public String toString(){

/* this.getX(): returns the X values stored in the subclass Rectangle
   this.getY(): returns the Y values stored in the subclass Rectangle
   getColor(): returns the color stored in the figure class. This is the
               method that will be defined by you.
*/

     return "x=" + this.getX() + ", y=" + this.getY() + ", color=" + getColor();

}
改变你的toString;方法如下:

figure类tostring方法超类

public String toString() {
    return "x=" + x + ", y=" + y + ", color=" + color ;
}
public String toString() {
    return "x-"+ x + "-y-" + y + "-color-" +color ;
}
这是矩形类tostring方法扩展的图形类

@Override
public String toString() {
    return "Rectangle [ "+  super.toString()   + ", width=" + width + ", length=" + length+  "]\n";
}
@Override
public String toString() {
    return super.toString()+"-width-" + width + "-length-"+length+ ";
}
然后使用正则表达式进行拆分-

然后从数组中赋值,如

String x = splittedString[1];
String y = splittedString[3];
String color = splittedString[5];
String width = splittedString[7];
String length = splittedString[9];

你是在问如何解析一个矩形格式的字符串吗[…返回到值?如果你是,那么你问题中的代码是无用的,字符串格式的示例将更加相关。是的,我希望x、y、长度和宽度的值通过矩形类将我的矩形画回,然后编辑你的问题。去掉我们不需要看到的代码。添加预期输入格式的示例预期输入是这样的[x=165,y=146,color=java.awt.color[r=255,g=0,b=0],width=83,length=79]我需要分别保存这些数值。编辑问题不要放在评论中。