gpt4 book ai didi

java - 编写了一个程序,但不断收到 ')' 、 ';' 和非法开始表达式错误? (java)

转载 作者:行者123 更新时间:2023-12-01 22:23:14 25 4
gpt4 key购买 nike

我必须编写一个程序,它接受一个名为目录的文本文件,并且我有两个类。类目录包含所有可能的操作,例如添加名称、检查名称是否存在、删除名称,然后将更新的目录写回文本文件。 DirectoryWithObjectDesign 类具有 main 方法,用于打开目录、处理用户交互并在完成后关闭目录。

这是 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");
}
}
}
}

这是类目录

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();
}
}

这些是我尝试运行 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

我做错了什么?

最佳答案

这一行有一个错误:

System.out.println("File is not found, exiting!", 
directoryFileName);

System.out.println() 仅接受一个参数。除此之外,如果您标记错误的位置将会很有帮助发生。

关于java - 编写了一个程序,但不断收到 ')' 、 ';' 和非法开始表达式错误? (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29281341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com