简体   繁体   English

Java扫描仪在线检测3个单词

[英]java scanner to detect 3 words on line

I am trying to write a little program that will use scanner to check if there is a next line (in awhile loop) and then maybe another one to check that the words on the line are tab appart and there are 3 strings (the use constructor to create a object) so the three strings would be Product Name Manufacturer Brcode 我正在尝试编写一个小程序,该程序将使用扫描程序来检查是否存在下一行(在while循环中),然后可能要检查另一行以确保该行上的单词是tab appart并且有3个字符串(use构造函数)创建一个对象),因此这三个字符串应为产品名称制造商Brcode

EG: Tyre17x60 Goodyear 458765464 EG:Tyre17x60固特异458765464

and so and so 等等

I am bit stuck with this so any help would be grateful 我对此感到有些困惑,所以不胜感激

You can try this: 您可以尝试以下方法:

  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];

    }
  }

Assuming you are reading from a file, you can use the following code: 假设您正在读取文件,则可以使用以下代码:

Scanner s = new Scanner(new File("file.txt"));
while(s.hasNext())
{
    String productName = s.next();
    String Manufacturer = s.next();
    String Brcode = s.next();
}
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());

        }

Try something like this.... 尝试这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM