samedi 28 mars 2015

Made a program but keep getting ')' , ';' , and illegal start of expression errors? (java)



I had to make a program that takes in a text file called directory and I have 2 classes. Class directory contains all the possible operations such as add a name, check to see if a name exists, delete a name, and then write the updated directory back to the text file. Class DirectoryWithObjectDesign has the main method, opens the directory, handles user interaction, and closes the directory when done.


Here is Class DirectoryWithObjectDesign:



import java.io.*;
import java.util.*;
public class DirectoryWithObjectDesign {
public static void main(String[] args) throws IOException {
String directoryDataFile = "Directory.txt";
Directory d = new Directory(directoryDataFile);
Scanner stdin = new Scanner(System.in);
System.out.println("Directory Server is Ready!");
System.out.println("Format: command name");
System.out.println("Enter ^Z to end");
while (stdin.hasNext()) {
String command = stdin.next();
String name = stdin.next();
if (command.equalsIgnoreCase("find")) {
if (d.inDirectory(name))
System.out.println(name + " is in the directory");
else
System.out.println(name + " is NOT in the directory");
}
else if (command.equalsIgnoreCase("add")) {
if (d.add(name))
System.out.println(name + " added");
else
System.out.println(name + " cannot add! " + "no more space or already in directory");
}
else if (command.equalsIgnoreCase("delete")) {
if (d.delete(name))
System.out.println(name + " deleted");
else
System.out.println(name + " NOT in directory");
}
else {
System.out.println("bad command, try again");
}
}
}
}


And here is Class directory



import java.util.*;
import java.io.*;
public class Directory {
public static void main(String[] args) {
final int maxDirectorySize = 1024;
String directory[] = new String[maxDirectorySize];
int directorySize = 0;
File directoryFile = null;
Scanner directoryDataIn = null;
Directory(String directoryFileName) {
directoryFile = new File(directoryFileName);
try {
directoryDataIn = new Scanner(directoryFile);
}
catch (FileNotFoundException e) {
System.out.println("File is not found, exiting!",
directoryFileName);
System.exit(0);
}
while (directoryDataIn.hasNext()) {
directory[directorySize++] = directoryDataIn.nextLine();
}
public boolean inDirectory(String name) {
for (int i = 0; i < directory.length; i++) {
if (directory[i].equalsIgnoreCase(name))
return true;
else
return false;
}
}
public boolean add(String name) {
if (directory.length == 1024)
return false;
for (int i = 0; i < directory.length; i++) {
if (directory[i].equalsIgnoreCase(name))
return false;
else
directory[directorySize++] = name;
return true;
}
}

public boolean delete(String name) {
for (int i = 0; i < directory.length; i++) {
if (directory[i].equalsIgnoreCase(name)) {
directory[i] = null;
return true;
}
else
return false;
}
}

public void closeDirectory() {
directoryDataIn.close();
PrintStream driectoryDataOut = null;
try {
directoryDataOut = new PrintStream(directoryFile);
}
catch (FileNotFoundException e) {
System.out.printf("File %s not found, exiting!", directoryFile);
System.exit(0);
}
String originalDirectory[] = {"Mike","Jim","Barry","Cristian","Vincent","Chengjun","susan","ng","serena"};
if (originalDirectory == directory)
System.exit(0);
else
for (int i = 0; i < directorySize; i++)
directoryDataOut.println(directory[i]);
directoryDataOut.close();
}
}


these are the errors when i try running DirectoryWithObjectDesign



Directory.java:10: error: ')' expected
Directory(String directoryFileName) {
^
Directory.java:10: error: illegal start of expression
Directory(String directoryFileName) {
^
Directory.java:10: error: ';' expected
Directory(String directoryFileName) {
^
Directory.java:23: error: illegal start of expression
public boolean inDirectory(String name) {
^
Directory.java:23: error: ';' expected
public boolean inDirectory(String name) {
^
Directory.java:23: error: ';' expected
public boolean inDirectory(String name) {
^
Directory.java:31: error: illegal start of expression
public boolean add(String name) {
^
Directory.java:31: error: ';' expected
public boolean add(String name) {
^
Directory.java:31: error: ';' expected
public boolean add(String name) {
^
Directory.java:43: error: illegal start of expression
public boolean delete(String name) {
^
Directory.java:43: error: ';' expected
public boolean delete(String name) {
^
Directory.java:43: error: ';' expected
public boolean delete(String name) {
^
Directory.java:54: error: illegal start of expression
public void closeDirectory() {
^
Directory.java:54: error: illegal start of expression
public void closeDirectory() {
^
Directory.java:54: error: ';' expected
public void closeDirectory() {
^
Directory.java:72: error: reached end of file while parsing
}
^
DirectoryWithObjectDesign.java:6: error: constructor Directory in class Directory cannot be applied to given types;
Directory d = new Directory(directoryDataFile);
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
DirectoryWithObjectDesign.java:15: error: cannot find symbol
if (d.inDirectory(name))
^
symbol: method inDirectory(String)
location: variable d of type Directory
DirectoryWithObjectDesign.java:21: error: cannot find symbol
if (d.add(name))
^
symbol: method add(String)
location: variable d of type Directory
DirectoryWithObjectDesign.java:27: error: cannot find symbol
if (d.delete(name))
^
symbol: method delete(String)
location: variable d of type Directory
Directory.java:10: error: cannot find symbol
Directory(String directoryFileName) {
^
symbol: variable String
location: class Directory
Directory.java:11: error: cannot find symbol
directoryFile = new File(directoryFileName);
^
symbol: variable directoryFileName
location: class Directory
Directory.java:17: error: cannot find symbol
directoryFileName);
^
symbol: variable directoryFileName
location: class Directory
Directory.java:26: error: incompatible types: unexpected return value
return true;
^
Directory.java:28: error: incompatible types: unexpected return value
return false;
^
Directory.java:31: error: variable name is already defined in method main(String[])
public boolean add(String name) {
^
Directory.java:33: error: incompatible types: unexpected return value
return false;
^
Directory.java:36: error: incompatible types: unexpected return value
return false;
^
Directory.java:39: error: incompatible types: unexpected return value
return true;
^
Directory.java:43: error: variable name is already defined in method main(String[])
public boolean delete(String name) {
^
Directory.java:47: error: incompatible types: unexpected return value
return true;
^
Directory.java:50: error: incompatible types: unexpected return value
return false;
^
Directory.java:58: error: cannot find symbol
directoryDataOut = new PrintStream(directoryFile);
^
symbol: variable directoryDataOut
location: class Directory
Directory.java:69: error: cannot find symbol
directoryDataOut.println(directory[i]);
^
symbol: variable directoryDataOut
location: class Directory
Directory.java:70: error: cannot find symbol
directoryDataOut.close();
^
symbol: variable directoryDataOut
location: class Directory
35 errors


What am I doing wrong??




Aucun commentaire:

Enregistrer un commentaire