"실제 및 형식 인수 목록 길이가 다른"얻기 나는 오류가 오류
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
나는이 오류를 받고 있어요 방법은 파일에서 읽을 예정이다. 코드를 게시하기 전에 코드에 대한 배경 지식을 제공 할 것입니다. 작고 간단한 주소록을 쓰고 있습니다. 나는 인수가없는 생성자와 getter와 setter가있는 "Contact class"를 가지고있다.
내 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");
}
}
}
}
이다 "문이"하나 개의 침 밖으로 오류입니다. 하지만 내 VectorofContact 클래스의 readInitialFromFile 메서드와 관련이 있습니다.
if(f.exists())
{
v.readInitialFromFile("contacts");
}
"contacts"
를 제거하고 : 이것은 당신이 시도하는 반면이 여기에서 String
매개 변수를 전달하기 위해, 매개 변수을지지 않습니다 것을 의미
public void readInitialFromFile() { /* implementation*/ }
:
연락처를 삭제했으나 결과물에 다음과 같은 이상한 내용이 나타납니다. "VectorofContact @ 3291384d" – oolostoo
@oolostoo 이는 Object의'toString()'의 기본 구현입니다. 사용자 지정 표현을 원한다면이 메서드를 재정의해야합니다. – August
어떻게 할 수 있습니까? – oolostoo