Solved

One simple compiler error

Posted on 2007-04-07
21
301 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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