I need help on why my code isn’t working.
Learn Intermediate Java | Codecademy
here is my code. Is something out of place or not included?
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Grep {
public Grep() {}
public static void main(String args) {
String fName;
File f;
Scanner s;
for (int fileNumber = 1; fileNumber <= 5; fileNumber++) {
fName = “TestFile” + fileNumber + “.txt”;
try {
f = new File(fName);
s = new Scanner(files[i]);
} catch (FileNotFoundException e) {
System.out.println(“Problem opening file.”);
e.printStackTrace();
return;
}
s.close();
}
Pattern pattern = Pattern.compile(“\w*\d+\w*”);
String line;
Matcher matcher;
while (s.hasNextLine()) {
line = s.nextLine();
matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println(fName + “:” + line);
}
}
}
}
and here are the errors I get:
Grep.java:22: error: cannot find symbol
s = new Scanner(files[i]);
^ //arrow is under ‘f’ in files
symbol: variable files
location: class Grep
Grep.java:22: error: cannot find symbol
s = new Scanner(files[i]);
^ //arrow is under ‘i’ after files
symbol: variable i
location: class Grep
2 errors
Hi there!
Could you make sure to use proper format when posting code to the forums?

It makes your code easier to read. 
But are you still having problems with your code?
Ah I did not know to use that thank you, and yes I am still having trouble.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Grep {
public Grep {}
public static void main(String[] args) {
String fName;
File f;
Scanner s;
for (int fileNumber = 1; fileNumber <= 5; fileNumber++) {
fName = "TestFile" + fileNumber + ".txt";
try {
f = new File(fName);
s = new Scanner(files[i]);
} catch (FileNotFoundException e) {
System.out.println("Problem opening file.");
e.printStackTrace();
return;
}
s.close();
}
Pattern pattern = Pattern.compile("\\w*\\d+\\w*");
String line;
Matcher matcher;
while (s.hasNextLine()) {
line = s.nextLine();
matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println(fName + ":" + line);
}
}
}
}
I figured out I had an extra ( ) I deleted, this is the last error I get:
rep.java:9: error: expected
public Grep {}
^
1 error
Well, if we take a look a your code you have:
public class Grep {
public Grep {}
}
You’re getting an error on public Grep because it is a constructor that is not declared correctly.
Here is the error code I receive, perhaps it will give you better idea:
Syntax error, insert "Identifier (" to complete MethodHeaderName
Ok, I have no idea what to put as the Identifier.
I am not sure of its use, but let’s think about this for a moment.
When you create a method, what does its structure look like?
I figured out what was wrong, I had pattern and matcher statements out of place and now it works.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Grep {
public Grep () {
}
public static void main(String[] args) {
String fName;
File f;
Scanner s;
for (int fileNumber = 1; fileNumber <= 5; fileNumber++) {
fName = "TestFile" + fileNumber + ".txt";
try {
f = new File(fName);
s = new Scanner(f);
String line;
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher;
while (s.hasNextLine()) {
line = s.nextLine();
matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println(fName + ":" + line);
}
}
} catch (FileNotFoundException e) {
System.out.println("Problem opening file.");
e.printStackTrace();
return;
}
s.close();
}
}
}
1 Like
Excellent! I try not to give too many direct hints, as I find working out a problem to be better in the learning process.
I am glad you got your code working. 
Yeah, I get you, but sometimes I need things to be explained to me like I’m 5. 
1 Like
I completely understand! That probably would have been my next move had you said you were still having problems. 
Now just keep chugging along, you’re doing great!