텍스트 파일의 모든 급여를 요약하는 데 코드를 가져 오는 데 문제가 있습니다. 저에게 평균 급여를 줄뿐만 아니라 모든 것을 합산 한 것입니다. 또한 출력 형식의 한 줄을 올바르게 포맷하는 데 문제가 있습니다. 제발 도와주세요, 전 아직 프로그래머에게 새로운입니다! 여기 내 주요 내용입니다. 여기 printf 및 텍스트 파일 읽기
import java.util.*;
import java.io.*;
public class fileIn
{
public static void main(String[] args)
{
String fName;
String lName;
String rank;
double salary = 0;
double newSal = 0;
Scanner inFile = null;
Employee[] emp = new Employee[20];
int numEmployee = 0;
// open the file
try
{
inFile = new Scanner(new File("employee.txt"));
} catch (FileNotFoundException e)
{
System.err.println("Error: File employee.txt not found ");
}
while (inFile.hasNext())
{
fName = inFile.next();
lName = inFile.next();
rank = inFile.next();
newSal = inFile.nextDouble();
salary += newSal;
emp[numEmployee] = new Employee(fName, lName, rank, salary);
numEmployee++;
}
System.out.println("Acme Corporation \n");
System.out.printf("Number Of Employees: %5d ", numEmployee);
System.out.printf("\nAverage Salary: %13.2f", salary/numEmployee);
System.out.printf("\nAnnual Total %16.2f", salary);
System.out.printf("\n\n%-8s %15s %10s", "Name", "Rank", "Salary\n");
for (int i = 0; i < numEmployee; i++)
{
System.out.printf("%s, %s \t%7s %11.2f\n", emp[i].getlName(),
emp[i].getfName(), emp[i].getRank(), emp[i].getSalary());
}
inFile.close();
}
}
및
은 샘플 출력입니다 :Acme Corporation
Number Of Employees: 9
Average Salary: 58740.50
Annual Total 528664.54
Name Rank Salary
------------------ ---- ---------
Jones, William B2 42500.00
Baker, Susan A3 107500.00
Caine, Horatio A1 191268.95
Baer, Teddy B4 244268.95
Gator, Allie A2 292268.95
Mander, Sally A1 354392.84
Aspargus, Amy A1 454442.84
Huckleberry, Henry B1 495677.34
Rutabaga, Ryan B2 528664.54
평균 급여 연간 총 완전히 잘못이다, 전체는 이상 2 백만해야하며, 평균 급여는 약 30 만 있어야한다. 고맙습니다!!
읽는 파일의 모습은 어떻습니까? 스캐너는'next()'는 전체 라인을 읽습니다. 여러분이 원하는 것은 아닐 수도 있습니다. – Felk
텍스트 파일은 출력과 비슷하게 포맷되지 않았습니다. 이름, 계급 및 급여 사이에는 공백이 있습니다. –
그러면 전체 파일은 1 줄이고 데이터는 공백으로 구분됩니까? 그런 다음 문자열에서 전체 파일을 읽고 공간에서 배열로 분할하여 파트에 액세스해야합니다.'String [] parts = string.split ("");' – Felk