2016-08-14 45 views
-3

我想用下面的任務開發java程序。在txt文件中搜索程序java代碼

使用輸入文件名並輸入產品代碼。並因此顯示所有產品細節

例如。

文件數據MLK##牛奶過期日期15 2016年5月

輸出將是

這個產品名稱是牛奶MLK代碼,並會在15可2016年

幫助過期我感謝...

我的代碼是...

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


public class search 
{ 

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


     String word = ""; int val = 0; 
     while(!word.matches("quit")) 
     { 
      System.out.println("Enter the word to be searched for"); 
      Scanner input = new Scanner(System.in); 
      word = input.next(); 
      Scanner file = new Scanner(new File("stationMaster.txt")); 

      while(file.hasNextLine())   
      { 
       String line = file.nextLine(); 
       if(line.indexOf(word) != -1) 
       { 
        while(file.hasNextLine()) 
        {  

         String data=file.nextLine(); 
         System.out.println(data); 
        } 


        //System.out.println(""); 
        val = 1; 
        break; 
       } 
       else 
       { 
        val = 0; 
        continue; 
       } 
      } 
      if(val == 0) 
      { 
      System.out.println("Station does not exist"); 
      break; 
      } 

     } 
    } 
} 

回答

2
package main; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class Main { 

    public static void main(String[] args) { 
     String tokens[] = null; 
     String code, product, date; 
     try { 
      FileReader fr = new FileReader("file.txt"); 
      BufferedReader br = new BufferedReader(fr); 
      String line = br.readLine(); 
      while (line != null) { 
       tokens = line.split("#"); 
       code = tokens[0]; 
       product = tokens[1]; 
       date = tokens[2]; 

       System.out.println("this product name is " + product 
         + " with " + code 
         + " code and will expired in" 
         + date.substring(12)); 
       line = br.readLine(); 
      } 
      br.close(); 
      fr.close(); 
     } catch (FileNotFoundException e) { 
      System.out.println("File not found exception."); 
     } catch (IOException e) { 
      System.out.println("IO Eception occered."); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 
+0

e.getMessage();爲這個人做任何事情,而不是編譯? – DevilsHnd

+0

好吧,我已經糾正:) –

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


public class search 
{ 
    public static void main(String[] args) throws IOException 
    { 
     String word = ""; int val = 0; 
     while(!word.matches("quit")) 
     { 
      System.out.println("Enter the word to be searched for"); 
      Scanner input = new Scanner(System.in); 
      word = input.next(); 
      Scanner file = new Scanner(new File("stationMaster.txt")); 

      while(file.hasNextLine())   
      { 
       String line = file.nextLine(); 

       //split the string on # character so that you get code, product name and expiration date separately. 

       String arr[] = line.split("#"); 

       //check whether the string contains the required string or not 

       try{ 
        if(arr[0].equalsIgnoreCase(word) || arr[1].equalsIgnoreCase(word)){         
         //line break 
         System.out.println(); 

         //split the format 'expiration date 15 may 2016' so that we can use date separately without the heading of 'expiration date' 
         String dateStrings[] = arr[2].split(" "); 

         System.out.print("this product name is " + arr[1] + " with " + arr[0] + " code and will expire on "); 
         System.out.println(dateStrings[2] + " " + dateStrings[3] + " " + dateStrings[4]); 
         val = 1; 
         break; 
        } 
        else 
        { 
         val = 0; 
         continue; 
        } 
       } 
       catch(IndexOutOfBoundsException indexEx){ 
        val = 0; 
        continue; 
       } 
      } 

      if(val == 0){ 
       System.out.println("Station does not exist"); 
       break; 
      } 
     } 
    } 
} 

上面的代碼將搜索將從文件中讀取的字符串,如果它包含要搜索的單詞。它可以是產品代碼或產品名稱

+0

你的try/catch來阻止異常? – DevilsHnd

+0

編輯代碼:) –

+0

我無法從該代碼中獲取日期 – Johny