2015-01-10 90 views
-4

当我尝试编译此代码我得到这个错误:Java方法名称错误

dn09.java:38: error: illegal start of expression 
       public Tip[] preberi (Scanner sc) { 
       ^dn09.java:38: error: ';' expected 
       public Tip[] preberi (Scanner sc) { 
            ^dn09.java:38: error: ';' expected 
       public Tip[] preberi (Scanner sc) { 
               ^3 errors 

[Napaka | process.javac]: Object reference not set to an instance of an object. 

这是代码的问题行:

public Tip[] preberi(Scanner sc) { 
      Tip[] tipi = new tipi[d]; 
      for (int i = 0; i < tipi.length; i++) { 
       String tip = sc.next(); 
        switch (tip) { 
         case "prim": 
          tipi[i] = new Prim(sc.nextInt()); 
          break; 
         case "arr": 
          tipi[i] = new Arr(sc.nextInt(), sc.nextInt()); 
          break; 
         case "ostruct": 
          break; 
         case "pstruct": 
          break; 
        } 
      } 
      return tipi; 
     } 

我有我的Scanner宣布了在main()方法,它是导入和一切。

你们有些人问这是我的整个代码(它在工作状态可言,你还会看到我是一个初学者所以其preety简单的心不是。

public class dn09 { 
    public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    int b = sc.nextInt(); 
    int d = sc.nextInt(); 
    Tip[] tipi = preberi(sc); 
    int u = sc.nextInt(); 
    int[] ukazi = new int[u]; 
    for (int i = 0; i < u; i++) { 
     ukazi[i] = sc.nextInt(); //if you know a better way to store 2 numbers where i could then 
           //use the numbers separately that would be super helpfull as id   
    }       //need it for 2 switch statements which im currenty trying to 
    for (int i = 0; i < u; i++) {//fit into 1. 
     switch(ukazi[i]) { 
      case 11: 
       break; 
      case 12: 
       break; 
      case 13: 
       break; 
      case 21: 
       break; 
      case 22: 
       break; 
      case 23: 
       break; 
      case 31: 
       break; 
      case 32: 
       break; 
      case 33: 
       break; 
     } 
    } 


    public Tip[] preberi(Scanner sc) { 
     Tip[] tipi = new tipi[d]; 
     for (int i = 0; i < tipi.length; i++) { 
      String tip = sc.next(); 
       switch (tip) { 
        case "prim": 
         tipi[i] = new Prim(sc.nextInt()); 
         break; 
        case "arr": 
         tipi[i] = new Arr(sc.nextInt(), sc.nextInt()); 
         break; 
        case "ostruct": 
         break; 
        case "pstruct": 
         break; 
       } 
     } 
     return tipi; 
    } 
} 

private static class Prim extends dn09 { 
    protected int v; 
    public static Prim (int v) { 
     this.v = v; 
    } 
} 

private static class Arr extends dn09 { 
    protected int n; 
    protected int t; 
    public static Arr (int n, int t) { 
     this.t = t; 
     this.n = n; 
    } 
} 

}

+1

你有这个方法在'main'方法也宣告? –

+0

粘贴你整个dn09类代码。 – SMA

+6

这个问题似乎是在你发布的代码之前。发布可运行的最小示例。此外,一般来说,您应该使用Eclipse等良好IDE来帮助解决编译时错误。 – wvdz

回答

1

你的主要()方法需要关闭}你只关闭环路和开关。

您还需要删除您内部类的构造函数(Prim(int)Arr(int,int))两个static,有一个悬空}最后,也许你想删除关闭它的那个?

如果您使用IDE并自动缩进代码,那么这些问题会很快显现。

public class dn09 { 
    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     int b = sc.nextInt(); 
     int d = sc.nextInt(); 
     Tip[] tipi = preberi(sc); 
     int u = sc.nextInt(); 
     int[] ukazi = new int[u]; 
     for (int i = 0; i < u; i++) { 
      ukazi[i] = sc.nextInt(); //if you know a better way to store 2 numbers where i could then 
      //use the numbers separately that would be super helpfull as id 
     }       //need it for 2 switch statements which im currenty trying to 
     for (int i = 0; i < u; i++) {//fit into 1. 
      switch(ukazi[i]) { 
       case 11: 
        break; 
... 
      } 
     } 
    } 

    public Tip[] preberi(Scanner sc) { 
     Tip[] tipi = new tipi[d]; 
     for (int i = 0; i < tipi.length; i++) { 
      String tip = sc.next(); 
      switch (tip) { 
       case "prim": 
        liki[i] = new Prim(sc.nextInt()); 
        break; 
... 
      } 
     } 
     return tipi; 
    } 

    private static class Prim extends dn09 { 
     protected int v; 
     public Prim (int v) { 
      this.v = v; 
     } 
    } 

    private static class Arr extends dn09 { 
     protected int n; 
     protected int t; 
     public Arr (int n, int t) { 
      this.t = t; 
      this.n = n; 
     } 
    } 
} 
+0

额外的}是因为我很努力地发布代码(刚开始使用该网站),即使从所有的构造函数中删除静态我继续得到相同的错误 – LukaTheLegend

+0

@LukaTheLegend你需要阅读我所有的答案(尤其是关于关闭主要方法)。我为你重新格式化并添加了一个大纲。顺便说一句:你需要Java7来打开字符串。 – eckes

+0

我现在得到一组错误,告诉我它找不到提示 – LukaTheLegend