Solved

One simple compiler error

Posted on 2007-04-07
21
288 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 91
Java Options in Zimbra Server 2 54
object oriented programming comparison 5 72
hibernate example using maven 12 42
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…
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…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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