2016-03-20 13 views
0

Eminim söyleyebileceksiniz, ama ben java'da yeniyim. Her halükarda, dosyadaki başka isimler ve bilgi olduğunda, bir txt dosyasından arama yapmak için bir addaki bir kişi türünden sonraki 3 satırı okumaya çalışıyorum. Bilgi, ad, soyadı, kimlik numarası ve yılların deneyimini soran ve bunları ayrı satırlara basan başka bir programdan txt dosyasına konuldu. Şimdiye kadar sahip olduğum şey bu.Bir .txt dosyasından bir ad okuduktan sonra sonraki 3 satırı nasıl yazdırabilirim?

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

Ayrıca 'for' gibi bir döngü de kullanabilirsiniz. – Akbari

+1

"Klavyeyi" okumadan "inputFile" değerini okumaya geçiş yapmış görünüyorsunuz. –

+0

For döngüsünü nasıl kullanacağımı düşünmeye çalışıyordum ama nasıl yapılacağını düşünemedim. – Ryfool

cevap

0

bir dosyaya burada

satırları okumak için yöntem benim sahte kod edilir .hasNext kullanabilirsiniz:

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. 

public class Abebe { public void ana (String [] args), FileNotFoundException değerini atar: 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); 
     } 
}} 
İlgili konular