我是第一次学习Java的学生,我有一个项目,我要求用户提供文本文件的文件路径。文本文件加载,在这种情况下,它导入有关租户(租房者)的信息,包括名字,姓氏,单位#,租金等。导入后,我应该为所有租户加上租金和显示平均支付的租金。我有一切正常,但我无法从文本文件中添加租金并显示平均值。请帮忙!
这是我完成所有工作的主要课程
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Program {
public Program(){
Tenants t = new Tenants();
System.out.print("What is the path of the address file?");
Scanner s = new Scanner(System.in);
String fileName = s.next();
HashMap<String, AddressEntry> addressMap = new HashMap<String, AddressEntry>();
File addressData = new File(fileName);
try{
FileReader fr = new FileReader(addressData);
BufferedReader br = new BufferedReader(fr);
//Call the constructor inside of Tenants class and displays average
t.Average();
String line = null;
while((line = br.readLine()) != null){
String [] entryData = line.split(",");
AddressEntry a = new AddressEntry();
a.firstName = entryData[0];
a.lastName = entryData[1];
a.unitNumber = entryData[2];
a.bedRoomQty = Integer.parseInt(entryData[3]);
a.bathRoomQty = Integer.parseInt(entryData[4]);
a.rent = Integer.parseInt(entryData[5]);
addressMap.put(a.firstName, a);
}
br.close();
}catch(FileNotFoundException fnfe){
System.out.println("There was an error in loading the addresses");
fnfe.printStackTrace();
}catch(IOException ioe){
System.out.println("There was an error in loading the addresses");
ioe.printStackTrace();
}
boolean askQuestion = true;
while(askQuestion){
System.out.println("Who would you like information about? \n");
System.out.println("Names: ");
for(Map.Entry<String, AddressEntry> a : addressMap.entrySet()){
System.out.println(a.getKey() );
}
String userAnswer = s.next();
if(userAnswer.equalsIgnoreCase("X")){
askQuestion = false;
System.out.println("Goodbye...");
}else{
if(addressMap.containsKey(userAnswer)){
System.out.println(addressMap.get(userAnswer));
}else{
System.out.println("That address name does not exist.");
}
}
}
}
}
以下是地址条目的代码
public class AddressEntry {
public String firstName = "NONE";
public String lastName = "NONE";
public String unitNumber = "NONE";
public int bedRoomQty = 0;
public int bathRoomQty = 0;
public int rent = 0;
public String toString(){
return "First name: " + firstName + "\n"
+ "Last Name: " + lastName + "\n"
+ "Unit Number: " + unitNumber + "\n"
+ "Bedroom QTY: " + bedRoomQty + "\n"
+ "Bathroom QTY: " + bathRoomQty + "\n"
+ "Rent: $" + rent+ "\n";
}
}
答案 0 :(得分:0)
如果您不熟悉编程,我建议您从设计过程开始,一旦完成编程就开始编程......
传递此块
String line = null;
while((line = br.readLine()) != null){
String [] entryData = line.split(",");
AddressEntry a = new AddressEntry();
a.firstName = entryData[0];
a.lastName = entryData[1];
a.unitNumber = entryData[2];
a.bedRoomQty = Integer.parseInt(entryData[3]);
a.bathRoomQty = Integer.parseInt(entryData[4]);
a.rent = Integer.parseInt(entryData[5]);
addressMap.put(a.firstName, a);
}
br.close();
所有租户信息都应该加载到addressMap中。所以,此时你应该遍历addressMap并开始累加租金......
double total = 0.0;
double sum = 0.0;
for(Entry<String, AdressEntry> entry : addressMap.entrySet())
{
total++;
sum = sum + entry.getValue().rent;
}
double average = sum / total;
所以,procces是:
再见!