簡體   English   中英

用拆分的JAVA解析字符串

[英]Parsing strings with split JAVA

我正在嘗試從文本文件中找到列表中的對象

例:

  • 升; 10; 10,50€; 83259875; YellowPaint
  • -H; U; 30; 12,00€; 98123742;錘
  • G; U; 80; 15,00€; 87589302;種子

通過使用掃描儀輸入插入98123742,我想找到該字符串。 我試圖這樣做:

private static void inputCode() throws IOException {

        String code;
        String line = null;
        boolean retVal = false;
        System.out.println("\ninsert code: ");
        code = in.next();
        try {

            FileReader fileReader = new FileReader("SHOP.txt");


            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((line = bufferedReader.readLine()) != null) {

                String[] token = line.split(";");

                if (token[0].equals(code) && token[1].equals(code)) {
                    retVal = true;
                    System.out.println(line);
                }
            }


            bufferedReader.close();
        } catch (FileNotFoundException ex) {
            System.out.println("impossible open the file " + fileName);
        }
         catch(IOException ex) {
                System.out.println(
                    "Error reading file '" 
                    + fileName + "'");                  

            }
           System.out.println(retVal);
        }

如何打印插入“ 98123742”(即產品代碼)的“ -H; U; 30;€12,00; 98123742; Hammer”?

你為什么首先分裂? 對於這樣一個簡單的用例,使用這種行格式,

line.contains(";" + code);

沒什么可做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM