Hi guys,

So I'm doing an assignment right now. One of the tasks is about listing files. It wants you to insert a name, either directory or a certain file, and give you a message with some informations. So I'm trying to finish the part where I enter a directory name, before I do the file part.

So my questions:
When I enter a directory/folder name, it wants me output these things:
- The name represententing a catalog.
- Total files and subfolders in the directory
- Total .java files in the folder
- Total java lines in the folder. (If I have a .java file with 200 lines, and another one with 23 lines, the output should be 223).

So I found total files and subfolders in the directory using this code, but it's kind of harder to find total .java files, and total lines.

My code for total .java files:

public void javaFiles(String file) {

		String files;
		File name = new File(file);
		File[] list = name.listFiles();

		for(int i = 0; i<list.length; i++) {

			if(list[i].isFile()) {

				files = list.getName();
				if(files.endsWith(".java") || files.endsWith(".JAVA"))
					output.append(files.length() + "\n");
			}
		}
}

This somehow doesn't work. Shouldn't I use files.length() when I want total sum of .java files?

Hi guys,

So I'm doing an assignment right now. One of the tasks is about listing files. It wants you to insert a name, either directory or a certain file, and give you a message with some informations. So I'm trying to finish the part where I enter a directory name, before I do the file part.

So my questions:
When I enter a directory/folder name, it wants me output these things:
- The name represententing a catalog.
- Total files and subfolders in the directory
- Total .java files in the folder
- Total java lines in the folder. (If I have a .java file with 200 lines, and another one with 23 lines, the output should be 223).

So I found total files and subfolders in the directory using this code, but it's kind of harder to find total .java files, and total lines.

My code for total .java files:

public void javaFiles(String file) {

		String files;
		File name = new File(file);
		File[] list = name.listFiles();

		for(int i = 0; i<list.length; i++) {

			if(list[i].isFile()) {

				files = list.getName();
				if(files.endsWith(".java") || files.endsWith(".JAVA"))
					output.append(files.length() + "\n");
			}
		}
}

This somehow doesn't work. Shouldn't I use files.length() when I want total sum of .java files?

I think not. To get the total of all *.java files you'd need a counter which increments whenever a java file is found... And to read lines you cant use .length either, youd have to open each file up read in line by line but of most importance would have to be having a counter to increase each time a new line is read in just like the total amount of java files counter

Total java lines

".length" will give you the length in characters, and not lines. So if you want to read the number of lines, I guess you'll have to open the files one-by-one and increase the counter for each file by 1 each time you encounter a semicolon.

Hope this helps! :)

So I managed to solve my problem. Thanks guys.
But now, I don't understand how I can count all java-lines in a folder. What I mean is if I have two java files, one with 200 lines and one with 136 lines, the output should be 336. Any tips for how I should begin?

if you read your file using bufferedReader, add +1 to a counter for each line in the file.

This is what I have:

else if (name.isDirectory()) // if the input is a directory
{ 

     String[] dir = name.list(); //list all files
     int lines = 0;
     for(int i = 0; i < dir.length; i++)
     {
	if(dir[i].endsWith(".java")) // if any files in directory ends with .java
	{
	    try(BufferedReader br = new BufferedReader(new FileReader(name))) {
		  while(br.readLine() != null)
		  {
			lines++;
		  }
			}
	     catch(IOException e3)
	     {

	     }
	     }
	}
	System.out.println(lines);
    }

Shouldn't this be right? I want to count all lines from multiple files in a directory :P

for every file, you are saying int lines = 0, so you won't get the lines from all the files at once.
what do you get as output, and what do you expect to get?

You don't show what is printed out by the program.
Showing the output would be helpful in determining the program's problems.

adding some println statements to check what your code is doing can help also. next to that:

catch(IOException e3){

}

what will you do if you actually get such an exception? maybe he doesn't find the file, and you just don't know, since you just disregard the exception.

add a System.out.println(e3.printStackTrace()); in there.

Not this:
System.out.println(e3.printStackTrace());
add this inside of the catch block:
e3.printStackTrace();

commented: busted :) +14

Not this:
System.out.println(e3.printStackTrace());
add this inside of the catch block:
e3.printStackTrace();

ahhh, yes off course, my bad :)
's what I get for answering a thread while having lunch :D

Keep both hands on the keyboard.

I put this code into the catch:

System.err.println( e3.getMessage() );
System.exit( 1 );

It tells me that I have no access to the folder. I'm expecting to get 5 as output since I have a java file in the folder with 5 lines. Shouldn't the while loop counter all java files and it's lines?

With e3.printStackTrace();
I get this:

java.io.FileNotFoundException: Skrivebord (Ingen tilgang)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileReader.<init>(FileReader.java:72)
	at Filtest.sjekkFil(Filtest.java:86)
	at Filtest$Tekstfeltlytter.actionPerformed(Filtest.java:112)
	at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
	at javax.swing.JTextField.postActionEvent(JTextField.java:721)
	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1661)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
	at java.awt.Component.processEvent(Component.java:6281)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4860)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1908)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:752)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1017)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:889)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:717)
	at java.awt.Component.dispatchEventImpl(Component.java:4730)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Window.dispatchEventImpl(Window.java:2713)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
	at java.awt.EventQueue.access$000(EventQueue.java:101)
	at java.awt.EventQueue$3.run(EventQueue.java:666)
	at java.awt.EventQueue$3.run(EventQueue.java:664)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:680)
	at java.awt.EventQueue$4.run(EventQueue.java:678)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
0
commented: I know someone who'll never forget to print his stacktraces again :) +14

.FileNotFoundException:

The system did not find your file.
Check location and spelling of the name.
What is the value of the name File.

The value of file is "Skrivebord", also a folder. I have my program outside the folder.

This is what I want as my output:
"The total number of lines java files containing altogether in Skrivebord."

Also I found out that if I write

try(BufferedReader br = new BufferedReader(new FileReader(Filetest.java))) // Filetest.java is my file which I'm working on outside "Skrivebord"

I get the output I should get, but I thought that the for loop should take me inside the folder, and with help of the if-sentence, it will choose all .java files and print out their lines :P

kind of doubt that line works ... if you add " 's, sure, but like that?
if you run it like that, what do you get as output?

kind of doubt that line works ... if you add " 's, sure, but like that?
if you run it like that, what do you get as output?

Sorry, but I don't understand xD What should I add where?

new FileReader(Filetest.java)

this wouldn't work

new FileReader("Filetest.java")

this on the other hand would :)

new FileReader(Filetest.java)

this wouldn't work

new FileReader("Filetest.java")

this on the other hand would :)

That works. But I want to count lines on multiple .java files. That's what I'm stressing with xD

well, have a loop:

int countLine = 0;
for ( File thisFile: fileList )
countLine += getNrOfLines(thisFile);
print countLine;

in your code, you have your int countLine = 0 within your loop, so it's been reset everytime

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.