0

I have been working on java since first week of october and I can't seem to continue this problem. I fix one problem and another keeps showing up. I am using Blue J and I am trying to add an array to the responses array. here is the code:

public class eDiary{
    public static void main (String args[]){
        int [] days = {1,2,3,4,5,6,7};
        String [] responses;
        int i = 0;

        for(i=0; i<7; i++){
            String response = Console.readString("What is your major event for day " + days[i]);
            responses[responses.length] = responses;

        }
    }


}

I am trying to make the user type in a major event for the day. Each event is supposed to add itself to the responses array as it corresponds to the days array (response 1 corresponds to day 1) I am not finished with the code but this is part one. The error keeps mentioning incompatible types on "responses[responses.length] = responses; How can I take care of this. There could be more errors as BlueJ seems to show them one at a time.

2
  • 2
    You need to concentrate on reading. Or maybe try naming your variables a bit differently. You're trying to assign into an index of the responses array...the responses array. Not the response String. Pay attention to the code, not just what BlueJ is telling you. Commented Oct 15, 2013 at 18:53
  • There are very, very few limits on the names you can assign to your variables. Why would you assign two unique variables such similar names? Commented Oct 15, 2013 at 19:01

2 Answers 2

5

In the line

 responses[responses.length] = responses;

responses is an array. You can only assign a String

May be you want to do this

 responses[responses.length] = response;

In the context of the question

Recommended Changes :

  1. Also, you should use i instead of responses.length:

    responses[i] = response;

  2. Initialize responses:

    String [] responses;

    To->

    String [] responses = new String[7]; // Provided 7 is fixed length.

Sign up to request clarification or add additional context in comments.

Comments

1

You never initialise responses, it is null at all times, most likely you want

String[] responses=new String[days.length]; //Assuming responses should be the same length as days

then you can add each response to this (now non null) array, so

   for(i=0; i<7; i++){
        String response = Console.readString("What is your major event for day " + days[i]);
        responses[i] = response; //Note response not responses

    }

Here you add a String into each of the 7 "boxs" of the array,

4 Comments

I like the way of notifying with comment. :)
@Sage I fear I may be missing something, what are you notifying me of?
uhh, i am not notifying you. i am just saying you are marking the error with comment, which i like. that is why gave you +1 :)
@Sage OH! My code comments! Thank you. I thought you ment stack overflow comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.