2014-10-23 2 views
-1

"실제 및 형식 인수 목록 길이가 다른"얻기 나는 오류가 오류

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*/ } 

:

답변

1

귀하의 readInitialFromFile 방법이 서명이 컴파일 할게.

public void readInitialFromFile(String str) { /* implementation*/ } 
+0

연락처를 삭제했으나 결과물에 다음과 같은 이상한 내용이 나타납니다. "VectorofContact @ 3291384d" – oolostoo

+1

@oolostoo 이는 Object의'toString()'의 기본 구현입니다. 사용자 지정 표현을 원한다면이 메서드를 재정의해야합니다. – August

+0

어떻게 할 수 있습니까? – oolostoo

0
public void readInitialFromFile() 

이 선언은 readInitialFromFile이 매개 변수를 사용하지 않는 것을 말한다 : 또 다른 해결책은 하나 개 String 매개 변수를 받아 readInitialFromFile의 서명을 수정하는 것입니다. 하지만 그 중 하나를 준 것입니다 :

v.readInitialFromFile("contacts"); 

이것은 가난한 컴파일러에게 혼란을줍니다. 당신의 readInitialFromFile 방법은

public void readInitialFromFile() 
{ 
    try 
    { 
     fin = new Scanner(new File("contacts.txt")); 
    } 
    ... 

처럼 보이는 때문에 당신이, 파일 이름 같은 문자열이 아니라 "contacts"을 통과 할 수 있도록 설정하려고 한 것 같습니다. 따라서 String name 매개 변수 (또는 원하는 매개 변수 이름)를 메서드에 추가 한 다음 메서드 본문에 new File을 사용할 때 해당 매개 변수를 사용하려고합니다.

일반적으로 "실제 및 형식 인수 목록의 길이가 다릅니다."라는 오류는 그 내용을 거의 의미합니다. 형식 인수 목록 (또는 매개 변수 목록)은 메소드를 작성할 때 선언하는 매개 변수를 나타냅니다. 귀하의 경우, 이것은 0 매개 변수입니다. 실제 인수 (매개 변수) 목록은 메서드를 호출 할 때 제공하는 매개 변수의 수를 나타내며, 귀하의 예제에서는 1입니다.

+0

내 말은 무슨 뜻인지 알겠지만 매개 변수에 매개 변수를 지정해야합니다. – oolostoo