2

I haven't coded in Java in many years, but recently I was asked to implement a java based program at work. Long story short, it did everything we needed, except it was placing the user ID in a report instead of the phone number. I should point out that the we are a partner of the company who owns the program, and I have their permission to change as needed. However, their website does not provide source code. I have requested it, but no luck after 3 weeks. So, I have to de-compile the code, and make the needed change. I have managed to make the change, and remove all compiling errors except three. They are all the same error on three different For Loops. When I attempt top compile I get the following.

FraudDetectionTool.java:655: not a statement
        for ( str3 : arrayOfString2 ) {
              ^
FraudDetectionTool.java:655: ';' expected
        for ( str3 : arrayOfString2 ) {
                  ^
FraudDetectionTool.java:655: illegal start of expression
        for ( str3 : arrayOfString2 ) {
                   ^
FraudDetectionTool.java:655: ';' expected
        for ( str3 : arrayOfString2 ) {
                    ^
FraudDetectionTool.java:681: not a statement
      for (str3 : array3) {
           ^
FraudDetectionTool.java:681: ';' expected
      for (str3 : array3) {
               ^
FraudDetectionTool.java:681: illegal start of expression
      for (str3 : array3) {
                ^
FraudDetectionTool.java:681: ';' expected
      for (str3 : array3) {
                 ^
FraudDetectionTool.java:695: not a statement
      for (str3 : arrayOfString3) {
           ^
FraudDetectionTool.java:695: ';' expected
      for (str3 : arrayOfString3) {
               ^
FraudDetectionTool.java:695: illegal start of expression
      for (str3 : arrayOfString3) {
                ^
FraudDetectionTool.java:695: ';' expected
      for (str3 : arrayOfString3) {

Here is the section of code that contains the first for loop. I left out the remaining portion of Main. If needed I can post the entire class (750 Lines) or Method (160 Line). What I have noticed is that str3 is initialized, but inside the For loop it is not recognized. However, if I leave the original initialization in place, and attempt to re-initialize inside the For Loop. My compiler warns me that the variable is already in use in Method Main.

public static void main(String[] paramArrayOfString)
{
 initializeProperties();
 init();

 GenericInputChannel.initialize(true);

 for (String str1 : paramArrayOfString) {
  fileNames.add(str1);
 }

 try
 {
   String[] mail = EMAIL_ADDRS.split(",");
   for (String str2 : mail) {
     str2 = str2.replaceAll(" *", "");
     emailAddresses.add(str2);
     if (str2.length() > 0) {
      if (DEBUG) {
            System.out.println("Adding email " + str2);
      }
      sendEmail = true;
    }
  }
 } 
 catch (Exception localException1) {
   System.out.println("Error parsing email addrs " + EMAIL_ADDRS);
   emailAddresses.clear();
 }

 if (TEST_MODE) {
  System.out.println("Sending Test email");
  sendEmail(true);
  System.exit(0);
 }

 String[] arrayOfString2 = new String[1];
 String str3;

 if (fileNames.size() == 0)
 {
 File localfiler = new File(DEFAULT_BILLING_DIR);
  if (((File)localfiler).exists()) {
    arrayOfString2 = ((File)localfiler).list();
    for ( str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);

    }
  }
 }
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
user2104894
  • 23
  • 1
  • 3

5 Answers5

5

When using the enhanced for loop, you have to declare the "loop variable" inside the for statement:

for (String s : list)

This similar code, on the other hand, won't compile:

String s;
for (s : list)

This is required by the language specification # 14.14.2:

EnhancedForStatement:
    for ( FormalParameter : Expression ) Statement
FormalParameter:
    VariableModifiersopt Type VariableDeclaratorId

(Note that Type is required in the declaration of the parameter)

assylias
  • 321,522
  • 82
  • 660
  • 783
3

change

String str3;

 if (fileNames.size() == 0)
 {
 File localfiler = new File(DEFAULT_BILLING_DIR);
  if (((File)localfiler).exists()) {
    arrayOfString2 = ((File)localfiler).list();
    for ( str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);

    }
  }
 }

to

 if (fileNames.size() == 0)
 {
 File localfiler = new File(DEFAULT_BILLING_DIR);
  if (((File)localfiler).exists()) {
    arrayOfString2 = ((File)localfiler).list();
    for (String str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);

    }
  }
 }
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
  • 2
    String being immutable has nothing to do with this. The enhanced `for` loop simply requires that the type of the loop variable be declared in the `for` statement itself. See, for instance, [this question](http://stackoverflow.com/questions/8204341/why-does-the-local-variable-of-an-enhanced-for-loop-have-to-be-local) – Ted Hopp Feb 24 '13 at 17:18
  • oh yes... sorry for mistace – Aliaksei Bulhak Feb 24 '13 at 17:20
  • @AlekseiBulgak, I attempted that, but then there are two other for loops in the same method that also use str3, and it left the same issue in those loops. Obviously, I do not want to re-initialize str3 in each loop. – user2104894 Feb 24 '13 at 17:30
  • I don't think you can do this without change for loop. if you want to use foreach statement you need to re-initialize str3 – Aliaksei Bulhak Feb 24 '13 at 17:35
1
for ( str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);

Must be instead

for (String str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);
1

Remove your String str3 and modify your for :

 for (String str3 : arrayOfString2 ) {
          fileNames.add(DEFAULT_BILLING_DIR + str3);
masoud
  • 55,379
  • 16
  • 141
  • 208
1

Your problem is when you are looping for str3 over arrayOfString2, beacuse you have to declare the str3 variable inside the loop.

Fixing your code like this should solve your problem.

    String[] arrayOfString2 = new String[1];

    if (fileNames.size() == 0)
    {
       File localfiler = new File(DEFAULT_BILLING_DIR);
       if (((File)localfiler).exists()) {
            arrayOfString2 = ((File)localfiler).list();
            for (String str3 : arrayOfString2 ) {
                  fileNames.add(DEFAULT_BILLING_DIR + str3);

            }
       }
    }

For more details, check The For Statement

araknoid
  • 3,065
  • 5
  • 33
  • 35