我正在尝试编写一个小程序,该程序将使用扫描仪检查是否有下一行(在一段时间循环中),然后可能另一个检查行上的单词是否是 tab appart 并且有 3 个字符串(使用构造函数创建一个对象),所以三个字符串将是产品名称制造商 Brcode
例如:轮胎 17x60 固特异 458765464
等等
我对此有点坚持,所以任何帮助将不胜感激
我正在尝试编写一个小程序,该程序将使用扫描仪检查是否有下一行(在一段时间循环中),然后可能另一个检查行上的单词是否是 tab appart 并且有 3 个字符串(使用构造函数创建一个对象),所以三个字符串将是产品名称制造商 Brcode
例如:轮胎 17x60 固特异 458765464
等等
我对此有点坚持,所以任何帮助将不胜感激
你可以试试这个:
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];
}
}
假设您正在从文件中读取,您可以使用以下代码:
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());
}
尝试这样的事情......