搜索CSV文件Java

时间:2013-11-21 16:41:23

标签: java file search csv

我尝试在csv文件中使用特定的数字字符串,即使文件存在,我仍然会收到一个FileNotFound异常。我似乎无法解决问题 示例Csv文件

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 个答案:

答案 0 :(得分:0)

使用:

  

import java.io.File;

     

import java.io.FileNotFoundException;

另外,在你的函数声明中尝试使用

public static boolean checkValidID() throws FileNotFoundException

同样具有主要功能。 如果文件存在且命名正确,则应该处理它。

相关问题