-2

I am trying to call my add method to add a score to my array I've got this so far, but I keep getting an error saying myQuiz was never initialized. ......................................................................................

import java.util.Scanner;
public class Assignment7 {
public static void main (String[] args) {
       //new scanner
       Scanner in = new Scanner (System.in);
       String choice;
       char command;
       // print the menu
       int count = 0;
       int counter = 0;
       printMenu();
       int array[] = new int[0];
      //do while loop testing using user input 
       do{
           // ask a user to choose a command
           System.out.println("\nPlease enter a command or type ?");
           choice = in.next().toLowerCase();
           command = choice.charAt(0);
           //start switch statement for user input cases
           switch (command)
            {
                 switch (command)
            {
                 case 'n':  //ask and read the size of the input array and name
                     System.out.print("\n\t n [Create a new Quiz]: ");
                     System.out.print("\n\t  [Input the size of quizzes]: ");
                     int num=in.nextInt(); // read the integer
                     array = new int[num];
                     System.out.print("\n\t  [Input the name of the student]: ");
                     String name = in.next(); //read name
                     Quiz myQuiz = new Quiz(num, name);
                      break;
                 case 'a': //  ask and add score to array
                      System.out.print("\n\t a [Add a score]: ");
                          array[count] = in.nextInt();
                          myQuiz.add(array[count]);
                          counter++;
                      break;
                     /*
                 case 'a': //  ask and add score to array
                      System.out.print("\n\t a [Add a score]: ");
                          array[count] = in.nextInt();
                          myQuiz.add(array[count]); //HELP
                          counter++;
                      break;

And my Quiz.java with add method

public class Quiz {

private int count;
private int[] scores;
private String name;    
public Quiz(int a,String name){
 scores = new int [a];
 for (int i = 0; i<scores.length; i++){
     scores[i] = -1;
 }
 this.count = 0;
 this.name = "";
 }
public void add(int b){
 for(int i : scores){
 if(scores[i] == count){
    System.out.println("Array is full. The value " + b + " cannot be added.");
 }
 else {
     scores[count] = b;
     count++;
 }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Lola Step
  • 1
  • 1
  • 1
    `myQuiz` is never defined in your main class `Assignment7` – Jens Nov 14 '14 at 08:03
  • I only see one mention of `myQuiz` and that's `myQuiz.add(array[count]);`. So, I'm guessing the error message is right – keyser Nov 14 '14 at 08:03
  • Somewhere before myQuiz.add(array[count]); you need to add Quiz myQuiz = new Quiz(); I leave it to you to figure out where to add the line :). – kha Nov 14 '14 at 08:04
  • I did add it, just left it out in this piece of code. Still gives me the error. Put that statement before the do-while loop – Lola Step Nov 14 '14 at 08:08
  • If you've made changes to your code, please edit the question to show them rather than making people guess what you have and have not tried. – Rook Nov 14 '14 at 09:10

2 Answers2

0

You create an empty array :

int array[] = new int[0];

and try to assign numbers to it :

array[count] = in.nextInt();

It can't work.

You must give it a positive length.

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Sorry, I left out this chunk where I did just what you said – Lola Step Nov 14 '14 at 08:05
  • So the length is defined by the user, should I give it a length (say 100) and then overwrite that length with user input? – Lola Step Nov 14 '14 at 08:10
  • @LolaStep You can give whatever length you are sure would be enough to store all the user input. You can't overwrite that length. Arrays in java have an immutable length. Once the length is set, you can only assign values to indices of the array (the valid indices are between 0 and array.length - 1) – Eran Nov 14 '14 at 08:34
0

Watch your scopes more carefully: You define myQuiz in one branch of your switch-case statement, but you access it in another branch, too. What happens, if case 'a' is accessed? The myQuiz variable is unknown there!

             case 'n':  //ask and read the size of the input array and name
                 System.out.print("\n\t n [Create a new Quiz]: ");
                 System.out.print("\n\t  [Input the size of quizzes]: ");
                 int num=in.nextInt(); // read the integer
                 array = new int[num];
                 System.out.print("\n\t  [Input the name of the student]: ");
                 String name = in.next(); //read name
                 Quiz myQuiz = new Quiz(num, name);
                  break;
             case 'a': //  ask and add score to array
                  System.out.print("\n\t a [Add a score]: ");
                      array[count] = in.nextInt();
                      myQuiz.add(array[count]);
                      counter++;
                  break;

You have to define myQuiz outside of your switch-case statement, before it, to be more specific.

user1438038
  • 5,821
  • 6
  • 60
  • 94