Solved

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

Posted on 2006-07-24
7
409 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 125 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 125 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 84
Oracle SQL syntax check  without executing 6 56
Configure a Bean in an XML file 4 32
how to install java on RHEL image on EC2 4 28
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

810 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