0

Building a survey application, and I am running into a roadblock with this problem. I am not understanding where my issue is exactly but when I am trying to create a two option menu, it is not allowing me to compile or run it.

errors:

Compiling 2 source files to C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\build\classes
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:71: error: cannot find symbol
            System.out.print("Enter text for question " + (i+1) + ": ");
  symbol:   variable i
  location: class Survey
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:75: error: cannot find symbol
            questions[i] = new IntegerQuestion(input.nextLine(),maxResponses);
  symbol:   variable i
  location: class Survey
2 errors
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:930: The following error occurred while executing this line:
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

Here's how I want it to be...

Choose from the following options: S - Create a question in a string N - Create a question in an integer

my current code:

package survey;

import java.util.Scanner;
import java.io.Serializable;

    public class Survey implements Serializable
    {
        private String surveyName;
        private Question[] questions;
        private int numQuestions;
        private int maxResponses;
        private boolean initialized;

        public Survey(String n)
        {
            surveyName = n;
            initialized = false;
        }


            //initialize() sets up the numQuestions, MaxResponses, and questions for the survey

        public char Questions()
        {
            Scanner input = new Scanner(System.in);

            System.out.println("Initializing survey \"" + surveyName + "\"\n");

            //add a method for password validation!?!?!?  yes!!!  see the bank accounts lab

            System.out.print("Enter max number of responses: ");
            maxResponses = input.nextInt();

            System.out.print("Enter number of questions: ");
            numQuestions = input.nextInt();

            input.nextLine(); //have to do this to "eat" the new line character or the next input won't work correctly
            System.out.println();

            questions = new Question[numQuestions];

            for(int i = 0; i < numQuestions;i++)
            {
               char choice;

            //output menu options
            System.out.println();      
            System.out.println("    S - Create String Question");
            System.out.println("    N - Create Integer Question");


            //loop until a valid input is entered

                System.out.print("Enter choice: ");
                choice = input.next().charAt(0);
                //if choice is one of the options, return it.  Otherwise keep looping
                if(choice == 'S' || choice == 'N'  )
                    return choice;
                else
                {
                    System.out.println("Invalid choice.  Ensure a capital letter. Please re-enter.");
                    choice = '?';
                }
           (choice == '?');

            return choice;  //will never get here, but required to have a return statement to compile   
        }
                System.out.print("Enter text for question " + (i+1) + ": ");

                //you will also need to ask what KIND of question - right now, defaults to integer question

                questions[i] = new IntegerQuestion(input.nextLine(),maxResponses);

            initialized = true;
        }


        /*
            run() gives the survey to a new survey taker, basically asks all the questions in the survey
        */
        public void startSurvey()
        {
            if(initialized)
            {
                System.out.println("Welcome to the survey \"" + surveyName + "\"\n");

                for(int i = 0;i < numQuestions; i ++)
                {
                    questions[i].askQuestion();
                }

                System.out.println("Thank you for participating!");
            }
            else
            {
                System.out.println("Survey has not yet been setup.  Please initialize first.");
            }

        }

        /*
            displayResults() displays the raw data for the survey
        */
        public void Results()
        { 
            System.out.println("Displaying data results for \"" + surveyName + "\"\n");

            for(int i = 0;i < numQuestions; i ++)
            {
                questions[i].displayResults();
                System.out.println();
            }
        }

        /*
            displayReportSummary() should run tests on your data 
            Examples could be: the most common response (median), the average response (mean), or display a graph of the results?
            The choices are endless!
        */
        public void reportSummary()
        {

        }


    }
bmargulies
  • 97,814
  • 39
  • 186
  • 310

1 Answers1

2

You're using i outside the loop. Because you declared i in the for loop, the scope of i is the loop only. It ceases to exist as soon as the loop ends.

The error messages from the compiler tell you which line of code the error is on, and exactly what the error is. It's worth learning to read these.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110