2013-11-21 74 views
0

我試圖在csv文件中指定一個特定的數字字符串,並且即使文件存在,我仍然會收到一個FileNotFound異常。我似乎無法修復該問題 檔案範例正在搜索一個CSV文件Java

12141895,LM051 12148963,Lm402 12418954,Lm876

用戶輸入:12141895

所需的結果:真

import javax.swing.*; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 

import java.util.*; 
import javax.swing.*; 
import java.io.*; 
import java.awt.*; 
import java.awt.List; 


public class tester 
{ 
public static void main (String [] args) throws IOException 
{ 

    boolean cool = checkValidID(); 
    System.out.println(cool); 
} 

public static boolean checkValidID() throws IOException 
{ 
    boolean results = false; 
    Scanner scan = new Scanner(new File("C:\\Users\\Packard Bell\\Desktop\\ProjectOOD\\IDandCourse.csv")); 
    String s; 
    int indexfound=-1; 
    String words[] = new String[500]; 
    String word = JOptionPane.showInputDialog("Enter your student ID"); 


    while (scan.hasNextLine()) 
    { 
    s = scan.nextLine(); 
    if(s.indexOf(word)>-1) 
     indexfound++; 

    } 
if (indexfound>-1) 
{ 
    results = true; 
} 
else 
{ 
    results = true; 
} 
return results; 
} 
} 
+1

請仔細檢查文件是否真的存在。 – Vogel612

+0

檢查**新文件(「C:\\ Users \\Packard Bell \\ Desktop \\ ProjectOOD \\ IDandCourse.csv」)。exists()* * – subash

+1

檢查你的文件名。有些人隱藏了擴展名,並錯誤地鍵入「filename.csv.csv」而沒有意識到他們的文件已經有了擴展名。 – MxyL

回答

0

使用:

import java.io.File;

import java.io.FileNotFoundException;

而且,在你的函數聲明嘗試使用

public static boolean checkValidID() throws FileNotFoundException 

,並同樣與主要功能。 如果文件存在並正確命名,則應該處理它。