繁体   English   中英

Java stackOverFlow错误对象编码方式错误

[英]Java stackOverFlow error Objects aren't coded the right way

我正在从事运输地图的项目。 我为该位置创建了一个名为Chart的对象和一个名为Pos的对象。 我无法工作的是,当我从文本文件中读取图表以正确创建对象Pos时,因为那是失败的地方。

现在是Pos班级

 private Pos Pos;

 public Pos(int d, int m, int t){
      Pos pos = new Pos(d,m,t);
 }

现在是Chart类

private String title;
private String colorString;
private String scale;
private String edition;
private Pos north;
private Pos south;
private Pos west;
private Pos east;

private Chart Chart;

public Chart(Pos north, Pos south, Pos west, Pos east, String color, String scale, String edition, String title){
    Chart = new Chart(north, south, west, east, color, scale, edition, title);
}

现在这里是我希望代码创建图表的地方。 我已经做了很多小步骤来确保输出正确:

In in;
    String fileName = "charts.txt";
    int aantalKaarten;
    int kolommen;
    String[][] tabel;
    int n1,n2,n3,s1,s2,s3,w1,w2,w3,e1,e2,e3;
    String color;
    String edition;
    String scale;
    String title;

    in = new In(fileName);
    aantalKaarten = in.readInt()+1;
    for(int i = 0;i < aantalKaarten;i++){
        try {
            n1 = in.readInt();
            n2 = in.readInt();
            n3 = in.readInt();
            System.out.println("Noordelijke lengtegraad: " + n1 + ", " + n2 + ", " + n3);
            s1 = in.readInt();
            s2 = in.readInt();
            s3 = in.readInt();
            System.out.println("Zuidelijke lengtegraad: " + s1 + ", " + s2 + ", " + s3);
            w1 = in.readInt();
            w2 = in.readInt();
            w3 = in.readInt();
            System.out.println("westelijke breedtegraad: " + w1 + ", " + w2 + ", " + w3);
            e1 = in.readInt();
            e2 = in.readInt();
            e3 = in.readInt();
            System.out.println("oostelijke breedtegraad: " + e1 + ", " + e2 + ", " + e3);
            color = in.readString();
            System.out.println("De kleur van de kaart: " + color);
            edition = in.readString();
            System.out.println("Editie: " + edition);
            scale = in.readString();
            System.out.println("Schaal: " + scale);
            title = in.readLine();
            System.out.println("Kaart: " + title);
            System.out.println("");
            Pos north = new Pos(n1,n2,3);
            Pos south = new Pos(s1,s2,s3);
            Pos west = new Pos(w1,w2,w3);
            Pos east = new Pos(e1,e2,e3);
            Chart chart = new Chart(north, south, west, east, color, scale, edition, title);
        }
        catch(NoSuchElementException eNSEE){
                System.out.println("No such element exception");
        }
    }
    in.close();

我真的希望我能得到一些帮助,因为我无可救药地陷入困境。 可能对Pos所做的更改我也必须更改为Chart,因为构建是相同的

先谢谢您的帮助。

编辑:这是我正在处理的文本文件

 20
 51 53 00 3 49 00 51 49 70 3 59 00 B ed:2011/2012 1:50.000 ANWB Z 5
 51 53 00 3 59 00 51 49 70 4 09 15 B ed:2011/2012 1:50.000 ANWB Z 6
 51 49 50 3 28 90 51 42 10 3 49 00 B ed:2011/2012 1:50.000 ANWB Z 7-8
 51 49 50 3 49 00 51 42 10 4 09 00 B ed:2011/2012 1:50.000 ANWB Z 9-10
 51 41 80 3 19 20 51 34 00 3 39 60 B ed:2011/2012 1:50.000 ANWB Z 11-12
 51 42 20 3 39 40 51 34 40 3 59 70 B ed:2011/2012 1:50.000 ANWB Z 13-14
 51 42 40 3 59 70 51 34 60 4 19 80 B ed:2011/2012 1:50.000 ANWB Z 15-16
 51 42 60 4 19 60 51 34 80 4 29 80 B ed:2011/2012 1:50.000 ANWB Z 17
 51 34 20 3 19 80 51 26 80 3 39 90 B ed:2011/2012 1:50.000 ANWB Z 19-20
 51 34 60 3 39 80 51 26 80 4 00 00 B ed:2011/2012 1:50.000 ANWB Z 21-22
 51 34 60 3 59 70 51 27 20 4 20 00 B ed:2011/2012 1:50.000 ANWB Z 23-24
 51 26 70 3 20 00 51 19 00 3 40 20 B ed:2011/2012 1:50.000 ANWB Z 25-26
 51 27 00 3 40 00 51 19 40 4 00 00 B ed:2011/2012 1:50.000 ANWB Z 27-28
 51 27 15 4 00 00 51 19 60 4 20 20 B ed:2011/2012 1:50.000 ANWB Z 29-30
 51 27 60 2 28 60 51 00 00 2 58 40 G ed:2012      1:100.000 Hydro 1801.2
 51 32 00 2 51 60 51 13 70 3 36 00 G ed:2012      1:100.000 Hydro 1801.3
 51 30 50 3 22 50 51 20 50 3 44 30 G ed:2012      1:50.000  Hydro 1801.4
 51 48 60 3 00 00 51 30 40 3 40 00 G ed:2012      1:100.000 Hydro 1801.5
 52 00 06 3 23 00 51 42 40 4 07 20 G ed:2012      1:100.000 Hydro 1801.6
 52 15 60 3 46 40 51 57 60 4 29 60 G ed:2012      1:100.000 Hydro 1801.7

在这里,在构造函数中它正在调用自身,所以有一个StackOverflowException

public Pos(int d, int m, int t){
     Pos pos = new Pos(d,m,t);<=here!
}

您的密码

public Pos(int d, int m, int t){
  Pos pos = new Pos(d,m,t);
}

运行此代码时,它将递归调用Pos构造函数本身,因此将其堆叠并调用内部Pos(),然后StackoverFlowException不会太奇怪。

你的课应该是

class Pos {
    int d;
    int m;
    int t;

    public Pos(int d, int m, int t) {
       this.d = d;
       this.m = m;
       this.t = t;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM