Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Returning an array from a constructor generates a .class expected error

Posted on 2006-07-24
7
Medium Priority
?
456 Views
Last Modified: 2010-03-31
I need help getting this code to function.

returning method in class Student compiles successfully

public String[] getter()
  {
    return new String[] {sIDNumber, sFirstName, sMiddleName, sLastName, sTitle, sSuffix, sDesignation, sStatus};
  }


Calling method generates the following errors

Lab3TestViewer.java:44: '.class' expected
                                String[] sParam = student1.getter();
                                         ^
Lab3TestViewer.java:44: not a statement
                                String[] sParam = student1.getter();

NOTE:  calls to student2.getter() and student3.getter() raise the same errors.

student1, student2, and student3 are successfully instantiated in the main of the class of which the calling method is a member.

private static void testStudentC(int iIn, String sMessageIn)
{
  String sMessage = "";
  if (iTestLevel > 1) System.out.println(sMessageIn);

  if (iTestLevel > 2)
  {
    if (iIn == 1)
      String[] sParam = student1.getter();
    else if (iIn == 2)
      String[] sParam = student2.getter();
    else if (iIn == 3)
      String[] sParam = student3.getter();

    sMessage =  + Utilities.buildSeries(sParam[]);
    if (sMessage.length() == 0) sMessage = "[NONE]";
    sMessage = "  Values:  " + sMessage;
    System.out.println(sMessage);
  }
}
0
Comment
Question by:athayes
7 Comments
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17169989
>> sMessage =  + Utilities.buildSeries(sParam[]);

are you not getting error here,

can you post the full code
0
 

Author Comment

by:athayes
ID: 17170254
Lab3TestViewer.java:44: '.class' expected
                                String[] sParam = student1.getter();

generates the error

the line you mentioned should read:

sMessage = Utilities.buildSeries(sParam[]);

I corrected it and still got the same errors for

I also changed it to:

sMessage = Utilities.buildSeries(sParam);

which stopped it getting the .class errors

I also commented out all of the sMessage code and still got the errors for student1.getter(). also 2 and 3


Here's the entire test class
/**
   This program tests CIS280 Lab3
*/
public class Lab3TestViewer
{
      public static int iTestLevel = 0;
      private static Student student1;
      private static Student student2;
      private static Student student3;
      public static void main(String[] args)
      {
            if (args.length == 0) iTestLevel = 0;
            else iTestLevel = Integer.parseInt(args[0]);

            //Test Student
            //      Test Constructors
            Student student1 = new Student();
               testStudentC(1,"Method - Constructor Student (without input)" );
            Student student2 = new Student("0001", "Ann", "Trowbridge", "Hayes","Mrs", "", "", "");
               testStudentC(2,"Method - Constructor Student (with standard input)" );
            String[] sArray1 = {"0002", "Michael", "Lynne", "Hayes", "Mr", "Sr","",""};
            Student student3 = new Student(sArray1);
            testStudentC(3, "Method - Constructor Student (with array input)");

            //  Test Setters
            String[] sArray2 = {"0003", "James", "Kemper", "Hayes", "Mr", "", "", ""};
            student1.setter(sArray2);
            testStudentS(1,"Method - Setter Student (with array input)");
            student2.setter(4,"Ms");
            testStudentS(2,"Method - Setter Student (with standard input)");
            //  Test Getters

            //  Test Other Methods
      }

      private static void testStudentC(int iIn, String sMessageIn)
      {
            String sMessage = "";
            if (iTestLevel > 1) System.out.println(sMessageIn);

               if (iTestLevel > 2)
            {
                  if (iIn == 1)
                        String[] sParam = student1.getter();
                  else if (iIn == 2)
                        String[] sParam = student2.getter();
                  else if (iIn == 3)
                        String[] sParam = student3.getter();

                     sMessage =  + Utilities.buildSeries(sParam[]);
                  if (sMessage.length() == 0) sMessage = "[NONE]";
                  sMessage = "  Values:  " + sMessage;
                   System.out.println(sMessage);
            }
      }

      private static void testStudentS(int iIn, String sMessageIn)
      {
            if (iTestLevel > 1) System.out.println(sMessageIn);

            if (iTestLevel > 2)
            {
                  switch (iIn)
                  {
                        case 1: String[] sParam = {student1.sIDNumber, student1.sFirstName,
                              student1.sMiddleName, student1.sLastName, student1.sTitle,
                              student1.sSuffix}; break;
                        case 2: String[] sParam = {student2.sIDNumber, student2.sFirstName,
                              student2.sMiddleName, student2.sLastName, student2.sTitle,
                              student2.sSuffix}; break;
                  }

                     sMessage =  + Utilities.buildSeries(sParam);
                  if (sMessage.length() == 0) sMessage = "[NONE]";
                  sMessage = "  Values:  " + sMessage;
                   System.out.println(sMessage);
            }
      }
}
0
 
LVL 13

Expert Comment

by:marchent
ID: 17173416
change all the statement like
>>Student student1 = new Student();
into
Lab3TestViewer.student1 = new Student();

and ur all statement like
>>String[] sParam = student1.getter();
into
String[] sParam = Lab3TestViewer.student1.getter();

~marchent~

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 500 total points
ID: 17173472
change your methods like this,

   private static void testStudentC(int iIn, String sMessageIn)
     {
          String sMessage = "";
          if (iTestLevel > 1) System.out.println(sMessageIn);

          String[] sParam ;
             if (iTestLevel > 2)
          {
               if (iIn == 1)
                     sParam = student1.getter();
               else if (iIn == 2)
                   sParam = student2.getter();
               else if (iIn == 3)
                   sParam = student3.getter();

                  sMessage =  + Utilities.buildSeries(sParam );
               if (sMessage.length() == 0) sMessage = "[NONE]";
               sMessage = "  Values:  " + sMessage;
                System.out.println(sMessage);
          }
     }

     private static void testStudentS(int iIn, String sMessageIn)
     {
          if (iTestLevel > 1) System.out.println(sMessageIn);

          String[] sParam ;
          if (iTestLevel > 2)
          {
               switch (iIn)
               {
                    case 1:  sParam =  new String[] {student1.sIDNumber, student1.sFirstName,
                         student1.sMiddleName, student1.sLastName, student1.sTitle,
                         student1.sSuffix}; break;
                    case 2: sParam = new String []{student2.sIDNumber, student2.sFirstName,
                         student2.sMiddleName, student2.sLastName, student2.sTitle,
                         student2.sSuffix}; break;
               }

               String sMessage ;
                  sMessage =   Utilities.buildSeries(sParam);
               if (sMessage.length() == 0) sMessage = "[NONE]";
               sMessage = "  Values:  " + sMessage;
                System.out.println(sMessage);
          }
     }


0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17173476
also , your main method,

  public static void main(String[] args)
     {
          if (args.length == 0) iTestLevel = 0;
          else iTestLevel = Integer.parseInt(args[0]);

          //Test Student
          //     Test Constructors
          student1 = new Student();
             testStudentC(1,"Method - Constructor Student (without input)" );
          student2 = new Student("0001", "Ann", "Trowbridge", "Hayes","Mrs", "", "", "");
             testStudentC(2,"Method - Constructor Student (with standard input)" );
          String[] sArray1 = {"0002", "Michael", "Lynne", "Hayes", "Mr", "Sr","",""};
          student3 = new Student(sArray1);
          testStudentC(3, "Method - Constructor Student (with array input)");

          //  Test Setters
          String[] sArray2 = {"0003", "James", "Kemper", "Hayes", "Mr", "", "", ""};
          student1.setter(sArray2);
          testStudentS(1,"Method - Setter Student (with array input)");
          student2.setter(4,"Ms");
          testStudentS(2,"Method - Setter Student (with standard input)");
          //  Test Getters

          //  Test Other Methods
     }
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17173478
can you find the chhanges made??
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 17173483
Declare it outside, like:

String[] sParam ;

if ( iIn == 1 )
  sParam = student1.getter () ;
else if ( iIn == 2 )
  sParam = student2.getter () ;
else if ( iIn == 3 )
  sParam = student3.getter () ;
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question