Question

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

Was it helpful?

Solution

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"});

OTHER TIPS

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" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top