2013-02-01 50 views
-1

背景:代碼應該通過csv文件(第二個鏈接),找到用戶名和密碼,並確認並顯示所有信息或寫入錯誤。但現在它只是說錯誤。提前歡呼。Java CSV讀取文件驗證不起作用

http://pastebin.com/YBpKRKe2 http://pastebin.com/9K3nwYG3

import java.io.*; 
import java.util.*; 

public class CSVRead 
{ 
    public static void main(String[] args) 
    throws Exception 
    { 

     BufferedReader CSVFile = 
      new BufferedReader(new FileReader("test123.csv")); 
     int invalidvar = 1; 

     Scanner input = new Scanner(System.in); 
     System.out.println("Enter your email"); 
     String email =input.nextLine(); 

     System.out.println("Enter your password"); 
     String password =input.nextLine(); 

     String dataRow = CSVFile.readLine(); // Read first line. 
     // The while checks to see if the data is null. If 
     // it is, we've hit the end of the file. If not, 
     // process the data. 

     while (dataRow != null) 
     { 
      String[] dataArray = dataRow.split("\\t"); 


      if ((dataArray[0].equals(email)) 
      &&(dataArray[1].equals(password))) 
      { 
       System.out.println("You email is " +dataArray[0]+"."); 
       System.out.println("You password is " +dataArray[1]+"."); 
       System.out.println("You first name is " +dataArray[2]+"."); 
       System.out.println("You second name is " +dataArray[3]+"."); 
       System.out.println("You street name is " +dataArray[4]+"."); 
       System.out.println("You city name is " +dataArray[5]+"."); 
       System.out.println("You postcode is " +dataArray[6]+"."); 
      } 

      else 
      { 
      System.out.println("Error"); 
      break; 
      } 


      dataRow = CSVFile.readLine(); 
     } 
     // Close the file once all data has been read. 
     CSVFile.close(); 

     // End the printout with a blank line. 
     System.out.println(); 

    } //main() 
} // CSVRead 
+0

請在此處郵政編碼 – smk

回答

0

你只檢查CSV頭之後的第一個記錄。您需要直到達到EOF繼續檢查記錄:

boolean found = false; 
while (!found && dataRow != null) { 
    String[] dataArray = dataRow.split("\\t"); 

    if ((dataArray[0].equals(email)) && (dataArray[1].equals(password))) { 
     System.out.println("You email is " + dataArray[0] + "."); 
      ... 
     found = true; 
    } 

    dataRow = csvFile.readLine(); 
} 

System.out.println("Result of CSV search: " + found); 

一些旁註: