Solved

One simple compiler error

Posted on 2007-04-07
21
277 Views
Last Modified: 2010-03-31
the Compiler error says this method must return a result of type char[] well the brackets line up and it does return a char[]. Please is there something I'm missing. Maybe I should make size and the char[] instance fields.?.?




import java.util.*;

public class Calculate {

      /**
       * @param args
       */
    //private int size = 0;
      //final int CAPACITY = 100000;
      //char[] processArray = new char[CAPACITY];
      
      public Calculate() {
      
    //int size = 0;
      //final int CAPACITY = 100000;
      //char[] processArray = new char[CAPACITY];


}
      
private static char[] preProcess(String process) {

      final int CAPACITY = 100000;
      char[] processArray = new char[CAPACITY];       
            final String VALID_SYMBOLS = "()1234567890^/*+-\t ";

try {      
      //String el =" ";      
      if (VALID_SYMBOLS.indexOf(process) == -1)

            throw new IllegalStateException();      
            
      else{      
            
            
            
                  //final int CAPACITY = 100000;
                  //char[] processArray = new char[CAPACITY];

                  char copyProcessArray[] = process.toCharArray();

                  for (int k = 0; k < copyProcessArray.length; k++) {

                        processArray[k] = copyProcessArray[k];

                  }
           
                  int size = processArray.length;
            // check array for space and fix *
                  for (int index = 0; index < size; index++) {

                        
                        if (process.charAt(index) == ' ') {

                              for (int i = index; i < size - 1; i++) {

                                    processArray[i] = processArray[i + 1];
                                    
                              }

                              size--;
                        }

                        
                        char[] aNumber = { '1', '2' };
                        int aNumberLength = 8;
                        
                  if (index != size - 1) {
                              for (int indexANumber = 0; indexANumber < aNumberLength;               indexANumber++) {
                                    if ((processArray[index] == aNumber[indexANumber] && processArray[index] == '(')
                                                || (processArray[index] == ')'
                                                && processArray[index] == '(')) {

                                          for (int i = size; i > index; i--) {

                                                processArray[i] = processArray[i - 1];
                                          }

                                          processArray[index] = '*';
                                          size++;

                                    }      
                                          
                              }
                                    
                      }

            }
      
      
}

      return processArray;            
 
}
catch (IllegalStateException exception) {
      System.out.println("Error: statement has invalid characters");
    }

}


public static void main(String[] args) {
      // TODO Auto-generated method stub

      // final int CAPACITY = 100000;
       Scanner console = new Scanner(System.in);
     System.out.println("Input a File ==> ");
     String inputFileName = console.next();

     
try {       
           FileReader reader = new FileReader(inputFileName);
         Scanner in = new Scanner(reader);
       
        /* PreProccesing */
        while(in.hasNextLine())
        {
              // take in the first line as a whole string
              String unProccessed = in.next();
                 
               char arrayPostProccessed[] = preProcess(unProccessed);
              
               int postSize = arrayPostProccessed.length;
              
              for (int n = 0; n < postSize;n++) {
              
             System.out.println(arrayPostProccessed[n]);                  
              
                 }      
        }
}

catch (IOException exception) {
    System.out.println("Error processing file: " + exception);
   }



}
}
0
Comment
Question by:Dirk6996
  • 9
  • 4
  • 4
21 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
There is no typed return after the catch clause.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
JAVA is smarter than you think: if an Exception is thrown you continue
the method but there is a blank (implied) void return.

Solution:
1) put a dummy return after the catch
2) fill 'processArray' with something useful in the catch and return it only after the catch,
:: not inside the try clause.
The latter meets style standards.

;JOOP!
0
 
LVL 10

Expert Comment

by:Jaax
Comment Utility
It is advisable to have only one return statement for a method
Your code above will compile fine, by removing the "return processArray;" from its current line and placing it as  the last statement of the method
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Jaax, don't copy my answers will you!!!!

;JOOP!
0
 
LVL 10

Expert Comment

by:Jaax
Comment Utility
sciuriware:
FYKI, yours and mine are not IDENTICAL. I said that ONLY ONE return statement is enuf (by transferring the return to the end), whilst you have asked to place ONE MORE. I have also recommended to have only one return statement in all methods as a best practice. I do read before post!!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
You can't read:

2) fill 'processArray' with something useful in the catch and return it only after the catch,
:: not inside the try clause.

See?

;JOOP!
0
 
LVL 10

Expert Comment

by:Jaax
Comment Utility
>>2) fill 'processArray' with something useful in the catch and return it only after the catch,
:: not inside the try clause.

In that case shudn't 'processArray'  be returned in the try block too ?
Which means you are specifying two returns. While I had proposed for only one return.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
No, only ONE concluding return!
The try block builds data up.
The catch clause corrects it.
There is only ONE return in that case.

;JOOP!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:Jaax
Comment Utility
My apologies !
I misread >>return it only after the catch as "return it within the catch block"
You deserve the points
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
.... if any points will be given.

No worries, I thought you hijacked the thread (happens often).
We agree about a good return style; that's important.

;JOOP!
0
 

Author Comment

by:Dirk6996
Comment Utility
still trying to  fix.
0
 

Author Comment

by:Dirk6996
Comment Utility
So the bottom of the method should look something like this still same Compiler error.


}

      //return processArray;            
 
}
catch (IllegalStateException exception) {
      System.out.println("Error: statement has invalid characters");
 processArray = null;    
}

return processArray;
}
0
 

Author Comment

by:Dirk6996
Comment Utility
If the exception is caught I would like to have it return null.
0
 

Author Comment

by:Dirk6996
Comment Utility
I can't believe that the site gave points without actually me doing it.
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 125 total points
Comment Utility
I cleaned up your code, formatted it to make it readable and deleted dead ends:

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 */
public class Calculate
{

   private static char[] preProcess(String process)
   {

      final int CAPACITY = 100000;
      char[] processArray = new char[CAPACITY];
      final String VALID_SYMBOLS = "()1234567890^/*+-\t ";

      try
      {
         //String el =" ";      
         if(VALID_SYMBOLS.indexOf(process) == -1)

            throw new IllegalStateException();

         //final int CAPACITY = 100000;
         //char[] processArray = new char[CAPACITY];

         char copyProcessArray[] = process.toCharArray();

         for(int k = 0; k < copyProcessArray.length; k++)
         {

            processArray[k] = copyProcessArray[k];

         }

         int size = processArray.length;
         // check array for space and fix *
         for(int index = 0; index < size; index++)
         {

            if(process.charAt(index) == ' ')
            {

               for(int i = index; i < size - 1; i++)
               {

                  processArray[i] = processArray[i + 1];

               }

               size--;
            }

            char[] aNumber =
            { '1', '2' };
            int aNumberLength = 8;

            if(index != size - 1)
            {
               for(int indexANumber = 0; indexANumber < aNumberLength; indexANumber++)
               {
                  if((processArray[index] == aNumber[indexANumber] && processArray[index] == '(')
                     || (processArray[index] == ')' && processArray[index] == '('))
                  {

                     for(int i = size; i > index; i--)
                     {

                        processArray[i] = processArray[i - 1];
                     }

                     processArray[index] = '*';
                     size++;

                  }

               }

            }

         }

      }
      catch(IllegalStateException exception)
      {
         System.out.println("Error: statement has invalid characters");
      }
      return processArray;

   }


   /** */
   public static void main(String[] args)
   {
      // TODO Auto-generated method stub

      // final int CAPACITY = 100000;
      Scanner console = new Scanner(System.in);
      System.out.println("Input a File ==> ");
      String inputFileName = console.next();

      try
      {
         FileReader reader = new FileReader(inputFileName);
         Scanner in = new Scanner(reader);

         /* PreProccesing */
         while(in.hasNextLine())
         {
            // take in the first line as a whole string
            String unProccessed = in.next();

            char arrayPostProccessed[] = preProcess(unProccessed);

            int postSize = arrayPostProccessed.length;

            for(int n = 0; n < postSize; n++)
            {

               System.out.println(arrayPostProccessed[n]);

            }
         }
      }

      catch(IOException exception)
      {
         System.out.println("Error processing file: " + exception);
      }

   }
}

Now in compiles.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Correction: "Now it compiles."
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
I would like to get A for this, as timeout bonus.

;JOOP!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

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

11 Experts available now in Live!

Get 1:1 Help Now