Solved

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

Posted on 2006-07-24
7
411 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
use lov values 2 63
Java exception bubble up 2 21
how to install java on RHEL image on EC2 4 41
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

831 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