java扫描程序在线检测3个单词

时间:2013-04-29 12:02:10

标签: java file java.util.scanner

我正在尝试编写一个小程序,它将使用扫描程序来检查是否有下一行(在一个循环中),然后可能是另一个程序来检查行上的单词是否为tab appart并且有3个字符串(使用构造函数来创建一个对象)所以这三个字符串将是Product Name Manufacturer Brcode

EG:Tyre17x60 Goodyear 458765464

等等

我有点困惑,所以任何帮助都会感激不尽

3 个答案:

答案 0 :(得分:2)

你可以试试这个:

  public static void main (String[] args) throws java.lang.Exception
  {
    String str = "Tyre17x60 Goodyear 458765464"; 
    InputStream is = new ByteArrayInputStream(str.getBytes());
    Scanner sc = new Scanner(is);
    sc.useDelimiter("\n");

    while (sc.hasNext())
    {         
      String[] tmp = sc.next().split("\t");
      if (tmp.length == 3)
         System.out.println("Text contains 3 parts separated with tabs");              
      else
        System.out.println("Text is not well formated");

      // save data 
      //productName = tmp[0];
      //manufacturer = tmp[1];
      //brcode = tmp[2];

    }
  }

答案 1 :(得分:1)

假设您正在读取文件,可以使用以下代码:

Scanner s = new Scanner(new File("file.txt"));
while(s.hasNext())
{
    String productName = s.next();
    String Manufacturer = s.next();
    String Brcode = s.next();
}

答案 2 :(得分:1)

Scanner scan = new Scanner("file.txt");
        while(scan.hasNext()){
            scan.nextLine();
            System.out.println("Product : " + scan.next());
            System.out.println("Name : " + scan.next());
            System.out.println("Code : " + scan.nextLong());

        }

尝试这样的事情......