英文:
How to skip a token in java, Scanner class
问题
以下是翻译好的部分:
我在 Stack Overflow 上搜索了一个能够理解的解决方案来解决我的问题,但是我没有找到一个。这个方法应该接受一个文本文件,并将信息放入一个对象数组中。
文本文件的格式如下:
1#约翰·多先生#出租车#34.7
3#杰克逊·塔米夫人#电话#20.5
一般的格式是
客人编号#客人姓名#原因#费用(整数 字符串 字符串 双精度浮点数)
该方法将信息放入一个名为 ExtraItems 类的对象数组 arrItems 中,该类用于跟踪由客人购买的额外费用,客人由其客人编号进行标识。
Guest 类的类变量为客人编号、原因、费用。我无法找到一种在创建类的实例时跳过客人姓名的方法,我一直在遇到错误。我尝试过 .skip() 方法,但出现了 NoSuchElementException 错误,我尝试过删除客人的姓名并为来自文本文件的行创建一个新的 Scanner 实例,但出现了 InputMismatchExpression 错误。我在下面附上了我的代码,该代码未考虑额外的标记。
public TestExtraItem(String textFile)
{
try
{
Scanner inputStream = new Scanner(new FileInputStream(textFile));
while (inputStream.hasNextLine() && counter < 100)
{
String line = inputStream.nextLine();
Scanner lineReader = new Scanner(line).useDelimiter("#");
arrItems[counter] = new ExtraItem(lineReader.nextInt(), lineReader.next(), lineReader.nextDouble());
lineReader.close();
counter++;
}
inputStream.close();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
以及我尝试解决的部分:
public TestExtraItem(String textFile)
{
try
{
Scanner inputStream = new Scanner(new FileInputStream(textFile));
while (inputStream.hasNextLine() && counter < 100)
{
String line = inputStream.nextLine();
Scanner lineReader = new Scanner(line).skip("Mr").useDelimiter("#");
arrItems[counter] = new ExtraItem(lineReader.nextInt(), lineReader.next(), lineReader.nextDouble());
lineReader.close();
counter++;
}
inputStream.close();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
我还有其他尝试,但这是我认为应该有效的尝试。
ExtraItems 类的构造函数如下:
public ExtraItem(int inGuestNum, String inItemType, double inCost)
{
guestNum = inGuestNum;
itemType = inItemType;
cost = inCost;
}
请问我可以得到一些帮助吗?我在下面附上了完整的 txt 文件。
1#Mr G Ferreira#Phone#7.05
2#Mrs L Honeywell#Drinks#71.95
3#Ms I Mendes#Kitchen#39.95
1#Mr G Ferreira#Kitchen#23.95
1#Mr G Ferreira#Drinks#7.15
4#Mr B Khoza#Taxi#127.25
5#Mrs I Freulich#Security#31.95
5#Mrs I Freulich#Internet#7.95
6#Mr J Kowalsky#Phone#18.30
英文:
I have searched Stack Overflow for an understandable solution to my problem but I have not found one. This method is supposed to take a text file and put the information in an Array of Objects.
The text file is formatted as follows:
1#Mr Jhon Doe#Taxi#34.7
3#Mrs Jackson Tammy#Phone#20.5
The general format is
GuestNumber#GuestName#Reason#Cost (int String String double)
The method puts the information into an array of objects, arrItems, of the ExtraItems class which keeps track of additional costs bought by a guest who is identified by their guest number.
The class variables for the Guest class are Guest Number, Reason, Cost. I cannot find a way to skip the Guest Name when creating instances of the class, and I keep getting errors. I have tried the .skip() method and got a NoSuchElementException, and I have tried removing the guest's name and making a new Scanner instance for the line from the text file, and I got a InputMismatchExpression. I have attached my code below which does not account for the extra token.
public TestExtraItem(String textFile)
{
try
{
Scanner inputStream = new Scanner(new FileInputStream(textFile));
while (inputStream.hasNextLine() && counter < 100)
{
String line = inputStream.nextLine();
Scanner lineReader = new Scanner(line).useDelimiter("#");
arrItems0+网站访问量 = new ExtraItem(lineReader.nextInt(), lineReader.next(), lineReader.nextDouble());
lineReader.close();
counter++;
}
inputStream.close();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
and my attempt at solving it`
public TestExtraItem(String textFile)
{
try
{
Scanner inputStream = new Scanner(new FileInputStream(textFile));
while (inputStream.hasNextLine() && counter < 100)
{
String line = inputStream.nextLine();
Scanner lineReader = new Scanner(line).skip("Mr").useDelimiter("#");
arrItems0+网站访问量 = new ExtraItem(lineReader.nextInt(), lineReader.next(), lineReader.nextDouble());
lineReader.close();
counter++;
}
inputStream.close();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
I had other attempts but this was the one I thought should work.
The constructor for the ExtraItems class is
public ExtraItem(int inGuestNum, String inItemType, double inCost)
{
guestNum = inGuestNum;
itemType = inItemType;
cost = inCost;
}
Please can I have some assistance? I have attached the full txt file below
1#Mr G Ferreira#Phone#7.05
2#Mrs L Honeywell#Drinks#71.95
3#Ms I Mendes#Kitchen#39.95
1#Mr G Ferreira#Kitchen#23.95
1#Mr G Ferreira#Drinks#7.15
4#Mr B Khoza#Taxi#127.25
5#Mrs I Freulich#Security#31.95
5#Mrs I Freulich#Internet#7.95
6#Mr J Kowalsky#Phone#18.30
专注分享java语言的经验与见解,让所有开发者获益!
评论