я получаю сообщение об ошибке:Попадая «фактические и формальные списки аргументов различаются по длине» ошибка
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
Метода Я получаю эту ошибку, предполагается чтение из файла. Прежде чем опубликовать свой код, я расскажу о своем коде. Я пишу небольшую и простую адресную книгу. У меня есть «Контактный класс», у которого есть конструктор no-arg и некоторые геттеры и сеттеры.
Комментарии к моему классу 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();
}
}
И это мой драйвер2 класс
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");
}
}
}
}
"если заявление" является один выплевывая ошибка. Но это имеет какое-то отношение к моему методу readInitialFromFile в моем классе VectorofContact.
Я удалил контакт, но теперь я получаю странные вещи на выходе: «VectorofContact @ 3291384d» – oolostoo
@oolostoo Это стандартная реализация 'toString()' в 'Object'. Вам нужно переопределить метод, если вы хотите создать собственное представление. – August
Как я могу это сделать? – oolostoo