Solved

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

Posted on 2006-07-24
7
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

751 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