我收到错误:
Driver2.java:22: error: method readInitialFromFile in class VectorofContact cannot be applied to given types;
v.readInitialFromFile("contacts");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
我得到此错误的方法应该从文件中读取。在发布我的代码之前,我将介绍一下我的代码。我正在写一本小而简单的地址簿。我有一个“Contact class”,它有一个no-arg构造函数和一些getter和setter。
我的Driver2课程的评论正在进行中,您可以忽略这些评论。
这是我的VectorofContact类:
import java.util.*;
import java.io.*;
import java.lang.*;
public class VectorofContact {
Contact c = new Contact();
private int size, capacity;
private Contact [] addressBook; // naming the vector
private Scanner fin;
private PrintWriter fout;
public VectorofContact()
{
addressBook = new Contact[12]; //12 cells
size = 0; // actual number of things in the cells
capacity = 12; //the amount of things the vector can hold
}
public void readInitialFromFile()
{
try
{
fin = new Scanner(new File("contacts.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("Can't read from file");
}
while(fin.hasNext())
{
for(int i = 0; i < size; i++)
{
String[] split= fin.nextLine().split(":");
String name = split[0];
long phoneNumber = Long.parseLong(split[1]);
String comment = split[2];
name = addressBook[0].getName();
phoneNumber = addressBook[1].getphoneNumber();
comment = addressBook[2].getComment();
}
}
}
public void deleteContact(String nM)
{
for(int i=0; i< size; i++)
{
if(addressBook[i].getName().equals(nM)) //delete the name the user wants to delete
{
addressBook[i] = addressBook[size - 1];
size --; //since something got deleted, size is decremented
return;
}
}
}
public void showByComment(String cM)
{
for(int i=0; i < size; i++)
{
if(addressBook[i].getComment().equals(cM))//reading the input from user
{
System.out.println(addressBook[size]);
}
}
}
public void showByName(String nM)
{
for(int i=0; i<size; i++)
{
if(addressBook[i].getName().equals(nM))
{
System.out.println(addressBook[size]);
}
}
}
public void save()
{
try
{
fout = new PrintWriter("contacts.txt");
}
catch (FileNotFoundException e)
{
System.out.println("Can not write file?!?");
return;
}
for (int i=0; i<size; i++)
{
fout.print(addressBook[i] + "\t");
}
fout.println();
fout.close();
}
}
这是我的Driver2类
import java.util.*;
import java.io.File;
public class Driver2
{
public static void main(String[] args)
{
VectorofContact v = new VectorofContact();
//orderedVectorofContact oV = new orderedVectorofContact();
File f = new File("contacts.txt");
if(f.exists())
{
v.readInitialFromFile("contacts");
}
Scanner input = new Scanner(System.in);
String commands;
boolean quit; //the quit command
boolean noContact; //if the conact doesn't exist in array.
quit = false;
noContact = false;
System.out.println("Welcome to Address Book");
System.out.println("Would you like to use a Vector or an Ordered Vector?");
while(!noContact)
{
System.out.println(v.toString());
commands = input.next();
switch(commands)
{
case "Vector" : System.out.println("Please enter your command: ");
break;
case "Ordered Vector" : System.out.println("Please enter your"
+ " command: "); break;
case "add": System.out.println("Name?");
//oV.addContact(input.nextLine());
System.out.println("Phone number?");
//oV.addContact(input.nextLong());
System.out.println("Comment?quit");
//oV.addContact(input.nextLine());
break;
case "Find name": System.out.print("What name would you like"
+ " to search?");
v.showByName(input.nextLine()); break;
case "Find comment": System.out.println("Please enter the "
+ " comment you wish to search: ");
v.showByComment(input.nextLine()); break;
case "remove" : System.out.println("What would you like to remove?");
System.out.println("Removed: " );
v.deleteContact(input.nextLine()); break;
case "quit" : quit = true;
System.out.println("Writing contacts.txt");
v.save(); break;
default: break;
}
if(noContact)
{
System.out.println("Contact does not extist/ has not been added");
}
}
}
}
“if语句”是吐出错误的那个。但它与我的VectorofContact类中的readInitialFromFile方法有关。
答案 0 :(得分:1)
您的readInitialFromFile
方法有此签名:
public void readInitialFromFile() { /* implementation*/ }
这意味着它接受无参数,而您尝试在此处传递一个String
参数:
if(f.exists())
{
v.readInitialFromFile("contacts");
}
删除"contacts"
并进行编译。另一种解决方案是修改readInitialFromFile
的签名以接受一个String
参数:
public void readInitialFromFile(String str) { /* implementation*/ }
答案 1 :(得分:0)
public void readInitialFromFile()
此声明表明readInitialFromFile
不带参数。但是你给了它一个:
v.readInitialFromFile("contacts");
这让糟糕的编译器感到困惑。由于您的readInitialFromFile
方法看起来像
public void readInitialFromFile()
{
try
{
fin = new Scanner(new File("contacts.txt"));
}
...
看起来您正在尝试设置它,以便您可以传入任何字符串作为文件名,而不仅仅是"contacts"
。因此,您可能希望向方法添加String name
参数(或任何您想要的参数名称),然后在方法体中使用new File
时使用该参数。
一般来说,“实际和形式的参数列表长度不同”错误意味着它所说的内容。形式参数列表(或参数列表)是指您在编写方法时声明的参数;在你的情况下,这是0参数。实际参数(参数)列表是指调用方法时给出的参数数量,在您的示例中为1。