문제

In my program the user declares a string of numbers that I am trying to figure out to turn into an array.
Example:

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

Im trying to figure out how to add that string into my class instance variable.
This is what I have:

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

My compile error message says incompatible types, required: String[], found: String

도움이 되었습니까?

해결책

It is because you have declared String[] strengths which is an array.

declare your constructor like this :

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

Make a call like :

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, new String[] {"27","26", "28"});

다른 팁

You need to parse that String of numbers to multiple Strings. For example,

this.strengths = strengths.split(",");

You can't say this.strengths = strengths because the strengths argument is of type String and not String[]. That is where your error is coming from.

Pass it like this:

WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, 
                          new String[] { "27", "26", "28" });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top