2017-03-31 56 views
0

我目前正在編寫一個代碼,它從文件中獲取信息,然後使用基於對象Request(我已經在另一個java文件中編寫)填充數組信息在文件中。這是我在我的main方法使用參數化構造函數添加到ArrayList

Scanner userIn = new Scanner(System.in); 
System.out.println("Please input the file name that you would like to be read into reqMover"); 
String thisFile = userIn.next(); 
FileReader inReader = new FileReader(thisFile); 
Scanner inFile = new Scanner(inReader); 

ArrayList<Request> newReq = new ArrayList<Request>(); 

do{ 
    newReq.add(Request(inFile.nextInt(), inFile.nextInt(), 
         inFile.nextInt(), inFile.nextInt())); 
    inFile.nextLine(); 
}while(inFile.hasNextLine()); 

此代碼駐留在被稱爲reqMover一個java文件至今。我遇到的這個問題是,編譯器告訴我「方法Request(int, int, int, int)對於類型reqMover未定義」,儘管該方法是我在其他類中創建的構造函數。使用這個構造函數加載ArrayList有什麼特別的需要嗎?

+1

你需要使用「特殊」的關鍵字的簽名:'新的請求(...)'。 –

回答

0

看起來您正在調用請求類的構造函數 要創建實例或調用annoymus對象,您需要使用新的運算符。 如果不是即你不調用構造函數,你需要定義一個方法與

Access-Specifier return type Request (type param1, type param2, type aram3, type param4){ 
    //handle your actions 
}