?
Solved

One simple compiler error

Posted on 2007-04-07
21
Medium Priority
?
308 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
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…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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