2017-01-12 4 views
2
import java.io.File; 
import java.util.Scanner; 

import java.io.*; 

public class Case1A { 

    private static Scanner scnCode; 

    public static void openFile() { 
     try{ 
      scnCode = new Scanner(new File("employee.txt")); 
     }catch(Exception e){ 
      System.out.println("You've got an error!"); 
     } 
    } 

    public static void readFile(String EmpCode){ 
     while(scnCode.hasNext()) { 
      String EmployeeCode = scnCode.next(); 
      String EmployeeName = scnCode.next(); 

     System.out.printf("Employee Name: %s\n", EmployeeName); 
     } 
    } 

    public static void closeFile() { 
     scnCode.close(); 
    } 


} 

TextFileeclipseの出力で表示されるテキストファイルを取得するには?

私はちょうどポストのようなテキストファイルを持っている、そしてプログラムは、特定のコードに対応する名前を取得する必要があります。たとえば、A11-0002を入力し、プログラムの出力はLamina、Seth M.となります。その特定のコードの名前はどのように取得できますか? 私のコードは上記にあり、間違ったコードがreadFile()メソッドにあり、正しいコードを取得できないと思います。

+1

コードにはどのような問題がありますか?あなたの 'main'メソッドはどこですか? – shmosel

+0

'EmployeeName = scnCode.nextLine()'を実行して名前全体を取得することを検討してください。 – shmosel

+1

個人的に私は行全体を読んで、コードと名前の間にある文字に基づいて分割します。 –

答えて

1

私はあなたが行うには、あなたのコードを望んでコーディングしようとしています: -

入力: - : 従業員名: - - スラジクマール

enter image description here

試験データ: - A11-11クマール、スラジュ A22出力 A11-11

-11 Laal、Baal A33-33 Teri、Warner

import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.*; 


    public class ParseText { 
static LinkedList<Employee> list = new LinkedList<>(); 

public static void main(String[] args) throws Exception{ 
    readFile(); 
    getEmployee("A11-11"); //This is for test, you get the 
          //read the id from command line as well 

    } 
public static void readFile() throws FileNotFoundException { 
    File file=new File("C:\\test\\textTest.txt"); 
    Scanner sc = new Scanner(file);  
    String temp; 
    while(sc.hasNext()){ 
     temp = sc.nextLine(); 
     //System.out.println("temp "+ temp); 
     String[] s = temp.split(" "); 
     String[] name = s[1].split(","); 
     String id = s[0]; 
     String lastName = name[0]; 
     String firstName = name[1]; 
     Employee emp = new Employee(firstName, lastName, id); 
     list.add(emp); 
    } 
    sc.close(); 
    } 

public static void getEmployee(String id) { 
    Iterator<Employee> itr = list.iterator(); 
    while(itr.hasNext()) { 
     Employee emp = itr.next(); 
     if(emp.id.equals(id)){ 
      System.out.println("Employee Name:- "+emp.firtName+" "+emp.lastName); 
     } 
    } 
} 
    } 

    class Employee { 
String firtName; 
String lastName; 
String id; 

Employee(String firstName, String lastName, String id) { 
    this.firtName = firstName; 
    this.lastName = lastName; 
    this.id = id; 
} 

public String getFirtName() { 
    return firtName; 
} 

public String getLastName() { 
    return lastName; 
} 

public String getId() { 
    return id; 
} 
    } 
+0

は何ですか?イテレータの意味?その目的は何ですか?私はちょうどプログラミングの新しいです! – Sethoy

+0

プログラミングの初心者であれば、Javaのオラクルのチュートリアル[リンク](https://docs.oracle.com/javase/tutorial/) を読んでみることをお勧めします。Javaコレクションでは、Iteratorインターフェース[リンク] https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html) –

+0

イテレータ・インタフェースには、hasNext()、next()およびremove()の3つのメソッドがあります。 一般に、hasNext()およびnext()は、 LinkedList、hasMap()などのさまざまなコレクションに実装されています。今あなたがイテレータを持っているならば、 は、スキャナの動作とほぼ同じように動作します。 hasNext()を使用すると、 は、コレクション内に次の要素が存在するかどうかを確認できます。 –

1

フィールドセパレータに基づいてファイルとそのラインレコードを解析し、マップキーの詳細をempcodeおよび従業員の詳細として値として保存できます。このようにして、empcodeをマップのキーとして渡すことから、雇用の詳細を取り出すことができます。

import java.io.File; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

public class Case1A { 
    private static String LINE_SEPERATOR = "\\t"; 

    public static void main(String[] args) throws Exception { 
     File file=new File("C:\\temp\\Employee.txt"); 
     Map<String, String> employeeMap = new HashMap<>(); 
     String lineData = null; 
     String[] empDetails = new String[10]; 
     Scanner sc = new Scanner(file); 
     while(sc.hasNextLine()){ 
      lineData = sc.nextLine(); 
      empDetails = lineData.split(LINE_SEPERATOR); 
      if(empDetails != null && empDetails.length >= 2){ 
       employeeMap.put(empDetails[0],empDetails[1]); 
      } 
     } 
     sc.close(); 
     System.out.println(employeeMap.toString()); 
    } 
} 
+0

質問に答えた場合は、答えとしてマークしてください。 –

+0

地図の意味は?それは何の目的ですか? – Sethoy

+0

私はちょうどプログラミングの新しいです。 – Sethoy

2
  1. あなたは、ファイルの内容を読み取るためにBufferedReaderを使用することができます。
  2. 最初のスペース文字の位置を検索します。
  3. str.substring()を使用してそのインデックスの行を分割します。今あなたは2つの弦を持っています。
  4. このキー値のペアをマップに配置できます。
  5. 地図にキーの値が必要なたびに、employees.get("A11-0003")を使用して取得してください。

出力:

Roda, Ronamy M. 

コード:

package apachecommonstest; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 

public class Case1A { 
    private static Map<String, String> employees = new HashMap<>(); 

    public static void main(String[] args) throws IOException { 
     setEmployeeData(); 
     System.out.println(employees.get("A11-0003")); 
    } 

    //set employee data from file to Map employees 
    private static void setEmployeeData() throws IOException { 
     BufferedReader br = null; 
     String line[] = new String[2]; 
     int spaceIndex; 
     try { 
      String sCurrentLine; 
      br = new BufferedReader(new FileReader("C:\\fakelocation\\employee.txt")); 
      while ((sCurrentLine = br.readLine()) != null) { 
       spaceIndex = sCurrentLine.indexOf(" "); 
       line[0] = sCurrentLine.substring(0, spaceIndex); 
       line[1] = sCurrentLine.substring(spaceIndex+1); 
       employees.put(line[0], line[1]); 
      } 
     } catch (IOException e) {} finally { 
      if(br!=null)br.close(); 
     } 
    } 
} 
+0

行レコードにempコードの後に​​スペースが1つしかない場合は、問題が発生します。例 - のような行レコードA11-230

+0

私はちょうど始める場所を与えました。さまざまな入力に応じてこのコードで実行できる多くの変更があります。 '' NullPointerException'などを探すために '' line [1] = sCurrentLine.substring(spaceIndex + 1); 'に' try-catch'ブロックを置くのと同じように... –

2

1)のFileInputStreamを使用してファイルを読み、その後、バッファリーダーを使用してファイルのメモリを作成します。 2)読み取​​りラインを使用してファイル内の行を読み取ります。 3)部分文字列を使って行を区切って比較して比較しました。

メイン:

public static void main(String[] args) { 
      Scanner scanner = new Scanner(System.in); 
      System.out.print("Please Enter: "); 
      String in = scanner.next(); 
      TextFile t = new TextFile(); 
      t.Employee(in); 
     } 

プログラム:

public class TextFile { 
    public void Employee(String in) { 
     FileInputStream f; 
     try { 
      f = new FileInputStream("D:/sample1.txt"); 
      BufferedReader br = new BufferedReader(new InputStreamReader(f)); 
      String strLine; 
      int space; 
      while ((strLine = br.readLine()) != null) { 
       space = strLine.indexOf(" "); 
       String s=strLine.substring(0,space); 
       String s1=strLine.substring(space+1); 
       if(in.equals(s)){ 
        System.out.println(s1.trim()); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

Output

sample text file

関連する問題