Issue
Question from "Java for Dummies", page 533-534, line 9,
It couldn't be compiled, line 9:
package Java_Dummy;
import java.io.File;
import java.util.Scanner;
public class DirList
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) // line 9strong text
{
System.out.print("Welcome to the Directory Lister");
do
{
System.out.print("\nEnter a path: ");
String path=sc.nextLine();
File dir = new File(path);
if (!dir.exists() || !dir.isDirectory())
System.out.println("\nThat directory does not exist.");
else
{
System.out.println("\nListing directory tree of:");
System.out.println(dir.getPath());
listDirectories(dir, " ");
}
}while(askAgain());
}
}
}
Solution
Your code misses curly braces, here's the fix version:
package Java_Dummy;
import java.io.File;
import java.util.Scanner;
public class DirList {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) // line 9strong text
{
System.out.print("Welcome to the Directory Lister");
do {
System.out.print("\nEnter a path: ");
String path = sc.nextLine();
File dir = new File(path);
if (!dir.exists() || !dir.isDirectory())
System.out.println("\nThat directory does not exist.");
else {
System.out.println("\nListing directory tree of:");
System.out.println(dir.getPath());
listDirectories(dir, " ");
}
} while (askAgain());
}
}
Is this the whole version though? You haven't declared "askAgain()" and "listDirectories()" methods
The rest is just fine and compiles successfully if you fix the brackets issue and declare or remove missing methods.
Answered By - Kloody
Answer Checked By - Timothy Miller (JavaFixing Admin)