2016-03-20 6 views
0

私はあなたが言うことができると確信していますが、私はjavaに新しいです。 とにかく私は人がファイルに他の名前と情報があるときにtxtファイルから検索するために名前をタイプした後に次の3行を読み込もうとしています。情報は、名字、姓、ID番号、年数を尋ねた別のプログラムからtxtファイルに入れられ、別々の行に印刷されました。これは私がこれまで持っていたものです。.txtファイルから名前を読み取った後、次の3行を印刷するにはどうすればよいですか?

System.out.println("Please enter first name of employee."); 
    String employeeName = keyboard.nextLine();  

    // Check to see if the name is in the file 
    // And for some reason if the name is in it, it says that it's not 
    if(fileName.contains(employeeName)) 
    { 
    System.out.println("Sorry, that employee is not in the file."); 
    } 
    else 
    {     
    // Read the last name. 
    String lastName = inputFile.nextLine(); 
    System.out.println(lastName); 

    // Read the employee number 
    String employeeID = inputFile.nextLine(); 
    System.out.println(employeeID); 

    // Read the years of experience 
    String years = inputFile.nextLine(); 
    System.out.println(years); 
    } 
+0

'for'のようなループを使うこともできます。 – Akbari

+1

'keyboard'の読み込みから' inputFile'の読み込みに切り替えたようです。 –

+0

私はforループの使い方を考えようとしていましたが、どのように考えるべきか分からなかった。 – Ryfool

答えて

0

ファイルここ

でラインを読むための方法を私の擬似コードさ.hasNext使用することができます。

else 
{ 

String[] name; 
int[] id; 
int[] yearsofexp; 

while (filename.hasNext()) 
      { 
       name = filename.nextLine(); 
       id = filename.nextInt; 
       yearsofexp = filename.nextInt(); 

      } 

} 
0
create a file "myFile.txt" and save it in the same folder with your code. 

パブリッククラスアベベは{ 公共の静的な無効メイン( String [] args)throws FileNotFoundException {

 Scanner keyboard= new Scanner(System.in); 

     System.out.println("Please enter first name of employee."); 


     String employeeName = keyboard.nextLine(); 

     File fileName= new File("myFile.txt"); // open the file 

     Scanner inputFile= new Scanner(fileName); // read in the file 

     if(!inputFile.hasNext(employeeName)) // here you are checking if the name you entered is available in your file 
     { 
      System.out.println("Sorry, that employee is not in the file."); 
     } 
     else 
     { 
      // Read the last name. 
      String lastName = inputFile.nextLine(); 
      System.out.println(lastName); 

      // Read the employee number 
      String employeeID = inputFile.nextLine(); 
      System.out.println(employeeID); 

      // Read the years of experience 
      String years = inputFile.nextLine(); 
      System.out.println(years); 
     } 
}} 
関連する問題