0
我試圖爲我們的項目建立一個工資系統,完成GUI和事件處理程序,但我被卡在文件處理。如何從陣列中打印特定的行?
我有一個文本文件,它看起來財產以後這樣
1 Miguel 491
2 Jasper 323
3 Will 363
我想要檢索的文本 輸出
1 Miguel 491
下面一行是我的代碼:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;
import java.awt.Label; import java.awt.event.*;
public class Main { public class panels extends JFrame{
}
public static void main(String[] args) {
JFrame frame = new JFrame("Payroll by Miggy");
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Label idlabel; // Declare an Label instance called lblInput
idlabel = new Label("Enter ID");
JButton jbtok = new JButton("Ok");
frame.add(jbtok);
try {
File f = new File("C:/Users/MIGXTREME/Desktop/Employee.txt");
Scanner sc = new Scanner(f);
List<Employee> people = new ArrayList<Employee>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = line.split(" ");
int Id = Integer.parseInt(details[0]);
String name = details[1];
int rate = Integer.parseInt(details[2]);
Employee p = new Employee(Id, name, rate);
people.add(p);
}
for(Employee p: people){
System.out.println(p.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} }
class Employee{
private int Id;
private String name;
private int rate;
public Employee(int Id, String name, int rate){
this.Id = Id;
this.setName(name);
this.rate = rate;
}
public int getId() {
return Id; }
public void setGender(int Id) {
this.Id = Id; }
public void setName(String name) {
this.name = name; }
public String getName() {
return name; }
public int getrate() {
return rate; }
public void setrate(int rate) {
this.rate = rate; }
public String toString(){
return this.Id + " " + this.name + " " + this.rate*4; } }
所以你面對什麼問題? – 2014-09-30 11:11:54
你能解釋一下怎麼回事?你做了什麼沒有奏效?另外,什麼是拋出一個錯誤?你的文本文件是如何寫入的?每行1人?或者更多?你需要給我們更多的細節,所以我們可以嘗試幫助你,而不是爲你做的工作。 – Softy 2014-09-30 11:13:21
實際上沒有錯誤,我只是想知道如何從文本文件中打印特定的行而不是所有的行.. – 2014-09-30 11:16:19