Java,从文件中读取两种不同类型的变量,稍后将它们用作对象
Java, Reading two different types of variables from a file and using them as objects later
我从事的项目基于从文件中读取文本并将其作为对象放入我的代码中。
我的文件包含以下元素:
(忽略要点)
- 4
- 圣诞晚会
- 20
- 情人节
- 12
- 复活节
- 5
- 万圣节
- 8
第一行声明了我的文本文件中有多少 "parties"(顺便说一句,它是 4 个)
每个派对都有两行 - 第一行是名称,第二行是可用的名额。
例如,圣诞派对有 20 个名额
这是我将文件中的信息保存为对象的代码。
public class Parties
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader ("C:\desktop\file.txt"));
int first = inFile.nextInt();
inFile.nextLine();
for(int i=0; i < first ; i++)
{
String str = inFile.nextLine();
String[] e = str.split("\n");
String name = e[0];
int tickets= Integer.parseInt(e[1]); //this is where it throw an error ArrayIndexOutOfBoundsException, i read about it and I still don't understand
Party newParty = new Party(name, tickets);
System.out.println(name+ " " + tickets);
}
这是我的单身派对Class:
public class SingleParty
{
private String name;
private int tickets;
public Party(String newName, int newTickets)
{
newName = name;
newTickets = tickets;
}
有人可以向我解释一下我该如何处理这个错误吗?
谢谢
nextLine()
returns 单个字符串。
考虑第一次迭代,例如,"Christmas Party"。
如果你用 \n
拆分这个字符串,你将得到的是长度为 1 的数组中的 "Christmas Party"。按“ 空白 space[ 拆分=19=]" 它应该可以工作。
str 只包含派对名称并且拆分它不起作用,因为它不会在那里有 '\n'。
循环内应该是这样的:
String name = inFile.nextLine();
int tickets = inFile.nextInt();
Party party = new Party(name, tickets);
// Print it here.
inFile().nextLine(); // for flushing
您可以创建一个 HashMap
并在迭代期间将所有选项放入其中。
HashMap<String, Integer> hmap = new HashMap<>();
while (sc.hasNext()) {
String name = sc.nextLine();
int tickets = Integer.parseInt(sc.nextLine());
hmap.put(name, tickets);
}
您现在可以对 HashMap
中的每个条目执行您需要的操作。
注意: 这假设您已经对文本文件的第一行做了一些操作,在您的示例中是 4
。
我从事的项目基于从文件中读取文本并将其作为对象放入我的代码中。
我的文件包含以下元素: (忽略要点)
- 4
- 圣诞晚会
- 20
- 情人节
- 12
- 复活节
- 5
- 万圣节
- 8
第一行声明了我的文本文件中有多少 "parties"(顺便说一句,它是 4 个) 每个派对都有两行 - 第一行是名称,第二行是可用的名额。
例如,圣诞派对有 20 个名额
这是我将文件中的信息保存为对象的代码。
public class Parties
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader ("C:\desktop\file.txt"));
int first = inFile.nextInt();
inFile.nextLine();
for(int i=0; i < first ; i++)
{
String str = inFile.nextLine();
String[] e = str.split("\n");
String name = e[0];
int tickets= Integer.parseInt(e[1]); //this is where it throw an error ArrayIndexOutOfBoundsException, i read about it and I still don't understand
Party newParty = new Party(name, tickets);
System.out.println(name+ " " + tickets);
}
这是我的单身派对Class:
public class SingleParty
{
private String name;
private int tickets;
public Party(String newName, int newTickets)
{
newName = name;
newTickets = tickets;
}
有人可以向我解释一下我该如何处理这个错误吗?
谢谢
nextLine()
returns 单个字符串。
考虑第一次迭代,例如,"Christmas Party"。
如果你用 \n
拆分这个字符串,你将得到的是长度为 1 的数组中的 "Christmas Party"。按“ 空白 space[ 拆分=19=]" 它应该可以工作。
str 只包含派对名称并且拆分它不起作用,因为它不会在那里有 '\n'。
循环内应该是这样的:
String name = inFile.nextLine();
int tickets = inFile.nextInt();
Party party = new Party(name, tickets);
// Print it here.
inFile().nextLine(); // for flushing
您可以创建一个 HashMap
并在迭代期间将所有选项放入其中。
HashMap<String, Integer> hmap = new HashMap<>();
while (sc.hasNext()) {
String name = sc.nextLine();
int tickets = Integer.parseInt(sc.nextLine());
hmap.put(name, tickets);
}
您现在可以对 HashMap
中的每个条目执行您需要的操作。
注意: 这假设您已经对文本文件的第一行做了一些操作,在您的示例中是 4
。