Solved

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

Posted on 2006-07-24
7
334 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
Comment Utility
>> sMessage =  + Utilities.buildSeries(sParam[]);

are you not getting error here,

can you post the full code
0
 

Author Comment

by:athayes
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
can you find the chhanges made??
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 125 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now