0

在我的程序中,用户声明了一串数字,我试图将其转换为数组。
例子:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, "27, 26, 28");

我试图弄清楚如何将该字符串添加到我的类实例变量中。
这就是我所拥有的:

   private String sampleID;
   private String weekOfTest;
   private int engineerID;
   private String[] strengths = new String[20];
   private static int count; 

  public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String strengths) 
   {
      this.sampleID = sampleID;
      this.weekOfTest = weekOfTest;
      this.engineerID = engineerID;
      this.strengths = strengths;
      count++;
   }

我的编译错误消息说类型不兼容,需要:String[],找到:String

4

4 回答 4

0

这是因为您已声明 String[]strengths 是一个数组。

像这样声明你的构造函数:

public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String[] strengths) 
   {
      this.sampleID = sampleID;
      this.weekOfTest = weekOfTest;
      this.engineerID = engineerID;
      this.strengths = strengths;
      count++;
   }

拨打电话:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, new String[] {"27","26", "28"});
于 2013-11-28T05:15:40.003 回答
0

像这样传递它:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, 
                          new String[] { "27", "26", "28" });
于 2013-11-28T05:15:43.683 回答
0

您需要将Stringnumbers 解析为多个Strings。例如,

this.strengths = strengths.split(",");
于 2013-11-28T05:16:15.537 回答
0

你不能说this.strengths = strengths是因为strengths参数是 typeString而不是String[]。那就是您的错误的来源。

于 2013-11-28T05:16:43.343 回答