我正在製作一個項目經理程序(一個非常簡單的程序)。基本上有CoWorkers和他們的任務。現在,我做了以下結構:我使用HashMap
鍵作爲CoWorkers的名稱,值始終是CoWorkers ArryList
,當然包含他或她的工作。使用ArrayList創建一個HashMap
我的問題是我可以創建這個結構,但是當我嘗試添加一個任務ArrayLists之一,我總是得到null
作爲返回值。
這裏是我的代碼片段:
HashMap<String, ArrayList<String>> jobs = new HashMap<String, ArrayList<String>>();
public void createJobs(){
nameReader();
for(String name:names){
File f = new File(name+"_job.txt");
if(f.exists()){
jobs.put(name, jobReader(name));
}
}
Set set = jobs.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
控制檯我看到以下結果此方法後:
喬治:[洗衣服]
所以我假設我做的一切都很好。
但後來:
public void addJob(String name, String job){
try{
List<String> itemsList = jobs.get(name);
itemsList.add(job);
}catch(Exception ex){
System.err.println(ex.getMessage() + jobs.get(name) +name);
}
}
,我調用此方法:
addjob("George","Clean up your room!");
控制檯上的結果將是:
nullnullGeorge
UPDATE
public ArrayList jobReader(String name){
ArrayList<String> tasks = new ArrayList<String>();
try{
Scanner s = new Scanner(new File(name+"_job.txt"));
while (s.hasNext()){
tasks.add(s.next());
}
s.close();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return tasks;
}
更新2
這是我的洞代碼:
public class CoWorkers {
HashMap<String, ArrayList<String>> jobs = new HashMap<String, ArrayList<String>>();
ArrayList<String> names = new ArrayList<String>();
public void createJobs(){
nameReader();
for(String name:names){
File f = new File(name+"_job.txt");
if(f.exists()){
jobs.put(name, jobReader(name));
}
}
Set set = jobs.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
public void addName(String name){
try{
String filename= "Names.txt";
FileWriter fw = new FileWriter(filename,true);
fw.write(name+"\n");
fw.close();
}
catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public void addJob(String name, String job){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try{
List<String> itemsList = jobs.get(name);
itemsList.add(job);
}catch(Exception ex){
ex.printStackTrace();
}
}
public ArrayList jobReader(String name){
ArrayList<String> tasks = new ArrayList<String>();
try{
Scanner s = new Scanner(new File(name+"_job.txt"));
while (s.hasNext()){
tasks.add(s.next());
}
s.close();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return tasks;
}
public void nameReader(){
names.clear();
try{
Scanner s = new Scanner(new File("Names.txt"));
while (s.hasNext()){
names.add(s.next());
}
s.close();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
public static void main(String[] args) {
CoWorkers project = new CoWorkers();
project.createJobs();
project.addJob("George","Do the laundry");
}
}
您的方法從哪裏調用? –
您是否可以在addJob方法中打印作業的內容,並在添加新作業時驗證內容仍然存在? – Farzad
另外,不是打印ex.getMessage(),而是調用ex.printStackTrace()。這往往是非常有益的 –