【发布时间】:2015-04-21 15:58:46
【问题描述】:
我正在尝试将以下句子作为字符串扫描到我的 Java 程序中:
The cat in the hat
The cat sat on the mat
Pigs in a blanket
然后使用whileloop 和hasNextLine()method 将其读入列表。
我的问题是我不确定如何读取它,因为它不是指定的文本文件,我必须使用args [0]
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class Scan {
public static void main(String[] args) throws FileNotFoundException{
//Opens a scanner into the file
File file = new File( args [0] );
try (Scanner scan = new Scanner(file))
{
ArrayList<String> list = new ArrayList<String>();
while(scan.hasNextLine())
{
list.add(scan.nextLine());
}
}
}
}
【问题讨论】:
-
你有什么问题?
-
我想我只是在输出它时遇到问题,只是为了检查并确保我做对了?当我运行程序时,我没有得到所需的文件输出
-
@Beginner
it is not a designated text file--> 您希望从哪里获得您的意见? -
作为命令行参数?我猜这不是我可以在 IDE @iRuth 中检查的东西?
标签: java file arraylist while-loop java.util.scanner