Java 从带有空格的文本文件中读取,然后放入arrayList
我花了一周的时间试图找出如何让这段愚蠢的代码正常工作。除了从我的文本文件中读取之外,我已经设法让所有的东西都工作了。它可以读取一行中的单个整数,但当给定一行包含多个由空格分隔的整数时,它会崩溃。现在我已经去尝试修复它,代码甚至不再编译。只有一条线路出现问题。 我不擅长编码,所以我不知道从哪里开始。是的,我在网上查过。是的,我查过论坛了。是的,我尝试了多种不同的方法来实现这一点。。。。 如何解决此问题??:(Java 从带有空格的文本文件中读取,然后放入arrayList,java,arraylist,bresenham,Java,Arraylist,Bresenham,我花了一周的时间试图找出如何让这段愚蠢的代码正常工作。除了从我的文本文件中读取之外,我已经设法让所有的东西都工作了。它可以读取一行中的单个整数,但当给定一行包含多个由空格分隔的整数时,它会崩溃。现在我已经去尝试修复它,代码甚至不再编译。只有一条线路出现问题。 我不擅长编码,所以我不知道从哪里开始。是的,我在网上查过。是的,我查过论坛了。是的,我尝试了多种不同的方法来实现这一点。。。。 如何解决此问题??:( ArrayList list=new ArrayList(); //上面这一行在同一个类
ArrayList list=new ArrayList();
//上面这一行在同一个类中的不同方法中,但在这里是相关的
File File=新文件(“C:\\Users\\Jocelynn\\Desktop\\input.txt”);
BufferedReader reader=null;
尝试
{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
而((text=reader.readLine())!=null)
{
//我希望下面的一行是“218 150 500 330”,并将每个整数存储到列表中。我不知道为什么它不起作用:(
add(Integer.parseInt(src.next().trim());
}
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
尝试
{
reader.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
//把清单打印出来
系统输出打印项次(列表);
谢谢你的帮助!我肯定我错过了一些非常简单的东西…在while循环中执行此操作
String[] individualArray = text.split(" ");//note space
for(String individual:individualArray){
yourList.add(individual);//You need to parse it to integer here as you have already done
}
在上面的代码中,individualArray将包含每个由空格分隔的整数。在for循环中,每个字符串都需要解析为整数,然后添加到列表中您可以使用
当然,您的整个方法可以通过使用和简化,就像
publicstaticvoidmain(字符串[]args){
File File=新文件(“C:\\Users\\Jocelynn\\Desktop\\input.txt”);
列表=新的ArrayList();
尝试(扫描器=新扫描器(文件);){
while(scanner.hasNextInt()){
添加(scanner.nextInt());
}
}捕获(例外e){
e、 printStackTrace();
}
//把清单打印出来
系统输出打印项次(列表);
}
试试这个:
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
// you need only this for loop in you code.
for (String value : text.split(" ")) { // get list of integer
if(!value.equals("")) // ignore space
list.add(Integer.parseInt(value)); // add to list
}
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
// print out the list
System.out.println(list);
}
publicstaticvoidmain(字符串[]args){
ArrayList=新建ArrayList();
File File=新文件(“C:\\Users\\Jocelynn\\Desktop\\input.txt”);
BufferedReader reader=null;
尝试
{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
而((text=reader.readLine())!=null)
{
//在代码中只需要这个for循环。
对于(字符串值:text.split(“”){//获取整数列表
如果(!value.equals(“”)//忽略空格
list.add(Integer.parseInt(value));//添加到列表
}
}
}catch(filenotfounde异常)
{
e、 printStackTrace();
}捕获(IOE异常)
{
e、 printStackTrace();
}
尝试
{
reader.close();
}捕获(IOE异常)
{
e、 printStackTrace();
}
//把清单打印出来
系统输出打印项次(列表);
}
while ((text = reader.readLine()) != null) {
Scanner scanner = new Scanner(text);
while (scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
}
public static void main(String[] args) {
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
List<Integer> list = new ArrayList<>();
try (Scanner scanner = new Scanner(file);) {
while (scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
} catch (Exception e) {
e.printStackTrace();
}
// print out the list
System.out.println(list);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
File file = new File("C:\\Users\\Jocelynn\\Desktop\\input.txt");
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
// you need only this for loop in you code.
for (String value : text.split(" ")) { // get list of integer
if(!value.equals("")) // ignore space
list.add(Integer.parseInt(value)); // add to list
}
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
// print out the list
System.out.println(list);
}