0

I am trying to get this to work but I keep getting errors, primarily

C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: '.class' expected
       Quiz myQuiz = new Quiz(int count, String name);
                                  ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: ';' expected
       Quiz myQuiz = new Quiz(int count, String name);
                                               ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: not a statement
       Quiz myQuiz = new Quiz(int count, String name);
                                                ^
C:\Users\Carter\OneDrive\Documents\Programs\Assignment7\Assignment7.java:26: error: ';' expected
       Quiz myQuiz = new Quiz(int count, String name);
                                                    ^
4 errors

Any help would be much appreciated! What I posted is just the driver program, I can also post the class definition if necessary! Thanks in advance!

//Driver program
import java.util.*;

public class Assignment7
{

   public static void main (String[] args) {

       Quiz myQuiz = new Quiz(int count, String name);

       Scanner console = new Scanner (System.in);

       String choice;
       char command;

       // print the menu
       printMenu();

       do
       {
           // ask a user to choose a command
           System.out.println("\nPlease enter a command or type ?");
           choice = console.next().toLowerCase();
           command = choice.charAt(0);

           switch (command)
            {
                 case 'n':  //asks and prints the quiz size and name of student
                      System.out.println("n [Create a new Quiz]");
                      System.out.println("[Input the size of quizzes]: ");
                      scores.length = console.nextInt();
                      System.out.print(scores.length);
                      System.out.println("[Input the name of student]: ");
                      name = console.nextString();
                      System.out.print(name);

                      break;
                 case 'a': //  adds a score
                      System.out.println("a [Add a score]: ");
                      numAdd = console.nextInt();
                      System.out.print(numAdd);

                      break;
                 case 'd': // deletes a score
                      System.out.println("d [Delete a score]: ");
                      numDelete = console.nextInt();
                      System.out.print(numDelete);

                     break;

                case 'p': //prints the information
                    System.out.println("p [Print the information]: ");
                    System.out.println(name);
                    System.out.println(scores);
                      break;


                case '?':
                      printMenu();
                      break;

                case 'q':
                      break;


                default:
                       System.out.println("Invalid input");

            }

        } while (command != 'q');

    }  //end of the main method


   public static void printMenu()
   {
    System.out.print("\nCommand Options\n"
                   + "-----------------------------------\n"
                   + "n: Create a new data\n"
                   + "a: Add a score\n"
                   + "d: Delete a score\n"
                   + "p: Print the information\n"
                   + "?: display the menu again\n"
                   + "q: Quit this program\n\n");

    } // end of the printMenu method

}
5
  • 1
    new Quiz(int count, String name); - You need a lesson in basic method calling syntax. Commented Nov 14, 2014 at 22:19
  • Well I am in a first semester java class so... And also, that is not calling a method, that is setting up the method object i am going to be using using the Quiz class. Commented Nov 14, 2014 at 22:19
  • How is Quiz defined? Do you want it to be the array you mention? Commented Nov 14, 2014 at 22:21
  • public class Quiz { private int count; private String name; private int[] scores; public Quiz (int count, String name2) { count = 0; name = name2; } Commented Nov 14, 2014 at 22:22
  • In addition what version of Java are you using? Switch statements won't work on strings on older versions of Java (pre JDK 7). stackoverflow.com/questions/338206/… Commented Nov 14, 2014 at 22:26

3 Answers 3

1

I think you should define the count and name before you call

Quiz myQuiz = new Quiz(int count, String name);

In addition, you don't need int and string identifier before you construct a Quiz object.

So basically, your code should be like

int count = **;
String name = "***";
Quiz myQuiz = new Quiz(count,name);
Sign up to request clarification or add additional context in comments.

Comments

0

When you call a method you don't need to specify the data types (only when defining a method)

e.g.

Quiz myQuiz = new Quiz(count, name);
//vs
Quiz myQuiz = new Quiz(int count, String name);

Since the data types are specified when you define the method, Java already knows what they are. You only need to pass the values.

4 Comments

Well I've tried that but I get like 10 more errors.
The other errors are likely something different. Try reading the errors to see if you can figure them out... or Google it.
Tried, i have like 30 minutes to finish this. Can I post them here maybe you could help ?
I hate to have to say this, but you need more than 30 minutes. I hope you figure it out.
0

Here's something that will COMPILE--ONLY to give you an idea of syntax and declaration requirements--but it won't come close to doing what you want. Good luck.

package javaapplication75;

public class Quiz {

 int count; 
 String name; 
 int[] scores; 

 public Quiz (int count, String name2) 
 { 
     count = 0;       // Not sure why 0...
     name = name2; }
 }


package javaapplication75;

    import java.util.*;

    public class Assignment7
    {
  private static int numAdd;  // need to declare these two vars
  private static int numDelete;


       public static void main (String[] args) {

           Quiz myQuiz = new Quiz(5, "D"); // doesn't do anything good, but compiles

           Scanner console = new Scanner (System.in);

           String choice;
           char command;

           // print the menu
           printMenu();

           do
           {
               // ask a user to choose a command
               System.out.println("\nPlease enter a command or type ?");
               choice = console.next().toLowerCase();
               command = choice.charAt(0);

               switch (command)
                {
                     case 'n':  //asks and prints the quiz size and name of student
                          System.out.println("n [Create a new Quiz]");
                          System.out.println("[Input the size of quizzes]: ");
                          myQuiz.count = console.nextInt()
                              ;
                          System.out.print(myQuiz.scores.length);
                          System.out.println("[Input the name of student]: ");
                          myQuiz.name = console.nextLine();
                          System.out.print(myQuiz.name);

                          break;
                     case 'a': //  adds a score
                          System.out.println("a [Add a score]: ");
                          numAdd = console.nextInt();
                          System.out.print(numAdd);

                          break;
                     case 'd': // deletes a score
                          System.out.println("d [Delete a score]: ");
                          numDelete = console.nextInt();
                          System.out.print(numDelete);

                         break;

                    case 'p': //prints the information
                        System.out.println("p [Print the information]: ");
                        System.out.println(myQuiz.name);
                        System.out.println(myQuiz.scores);
                          break;


                    case '?':
                          printMenu();
                          break;

                    case 'q':
                          break;


                    default:
                           System.out.println("Invalid input");

                }

            } while (command != 'q');

        }  //end of the main method


       public static void printMenu()
       {
        System.out.print("\nCommand Options\n"
                       + "-----------------------------------\n"
                       + "n: Create a new data\n"
                       + "a: Add a score\n"
                       + "d: Delete a score\n"
                       + "p: Print the information\n"
                       + "?: display the menu again\n"
                       + "q: Quit this program\n\n");

        } // end of the printMenu method


    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.