2014-09-30 2 views
0

GUI 및 이벤트 처리기로 완성 된 프로젝트의 급여 시스템을 구축하려고하지만 파일 처리가 어려워졌습니다.배열에서 특정 줄을 인쇄하려면 어떻게합니까?

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; } } 
:

는 내가 텍스트 출력 여기

1 Miguel 491 

에서 한 줄을 검색 할이

1 Miguel 491 
2 Jasper 323 
3 Will 363 

처럼 뭔가를해야만 보이는 텍스트 파일을하는 것은 내 코드입니다

+0

그래서 당신이 직면하는 어떤 문제? –

+0

무엇이 잘못되었는지 설명해 주시겠습니까? 그 didnt 일을 당신은 무엇을 시험해 보았 느냐? 또한 오류를 던지고있는 것은 무엇입니까? 텍스트 파일은 어떻게 작성됩니까? 한 사람당 1 명? 이상? 우리는 당신을 돕기 위해 노력할 수 있도록 우리에게 더 많은 세부 사항을 제공해야합니다. 당신을 위해 일하지 마십시오. – Softy

+0

잘 실제로 오류가 없다, 난 그냥 모든 줄이 아닌 텍스트 파일에서 특정 줄을 인쇄하는 방법을 알고 싶다. 파일에서 탐색하지 않고 –

답변

0

RandomAccessFile 클래스를 살펴보십시오. 그러면 비 순차적으로 파일을 탐색하는 데 도움이됩니다.

0

은 수행

for(Employee p: people){ 
    if(p.name.equals("Miguel")){ 
     System.out.println(p.toString()); 
     break; 
    } 
} 
관련 문제