Solved

One simple compiler error

Posted on 2007-04-07
21
291 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
ID: 18869190
There is no typed return after the catch clause.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18869198
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
ID: 18869308
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 24

Expert Comment

by:sciuriware
ID: 18869644
Jaax, don't copy my answers will you!!!!

;JOOP!
0
 
LVL 10

Expert Comment

by:Jaax
ID: 18869730
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
ID: 18869732
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
ID: 18869738
>>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
ID: 18869744
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
 
LVL 10

Expert Comment

by:Jaax
ID: 18869769
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
ID: 18869860
.... 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
ID: 18870168
still trying to  fix.
0
 

Author Comment

by:Dirk6996
ID: 18870188
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
ID: 18870194
If the exception is caught I would like to have it return null.
0
 

Author Comment

by:Dirk6996
ID: 18870312
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
ID: 18871648
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
ID: 18871982
Correction: "Now it compiles."
0
 
LVL 24

Expert Comment

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

;JOOP!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 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