我正在研究一个Java程序,它逐行读取一个文本文件,每个都有一个数字,每个数字都将它抛入一个数组,然后尝试使用插入排序对数组进行排序.我需要帮助让程序读取文本文件.
我收到以下错误消息:
Run Code Online (Sandbox Code Playgroud)java.io.FileNotFoundException: 10_Random (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at insertionSort.main(insertionSort.java:14)
我在我的"src""bin"和主项目文件夹中有.txt文件的副本,但它仍然无法找到该文件.我顺便使用Eclipse.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class insertionSort {
public static void main(String[] args) {
File file = new File("10_Random");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
int i = sc.nextInt();
System.out.println(i);
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我的csv正在读入System.out,但我注意到任何带空格的文本都会被移动到下一行(作为返回\n)
这是我的csv开始的方式:
first,last,email,address 1, address 2
john,smith,blah@blah.com,123 St. Street,
Jane,Smith,blech@blech.com,4455 Roger Cir,apt 2
Run Code Online (Sandbox Code Playgroud)
运行我的应用程序后,任何带有空格(地址1)的单元格都会被抛到下一行.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
// -define .csv file in app
String fileNameDefined = "uploadedcsv/employees.csv";
// -File class needed to turn stringName to actual file
File file = new File(fileNameDefined);
try{
// -read from filePooped with Scanner class
Scanner inputStream = new Scanner(file);
// hashNext() loops line-by-line
while(inputStream.hasNext()){
//read single line, put in string
String …Run Code Online (Sandbox Code Playgroud) 我试图从在线文本文件中读取一些单词.
我尝试过做这样的事情
File file = new File("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner scan = new Scanner(file);
Run Code Online (Sandbox Code Playgroud)
但它没有用,我得到了
http://www.puzzlers.org/pub/wordlists/pocket.txt
Run Code Online (Sandbox Code Playgroud)
作为输出,我只想得到所有的话.
我知道他们在当天教我这个,但我现在不记得到底是怎么做的,非常感谢任何帮助.
我正在System.in使用一个用户输入java.util.Scanner.我需要验证以下内容的输入:
最好的方法是什么?
所以,我对这段代码感到困惑:
import java.util.InputMismatchException;
import java.util.Scanner;
public class ConsoleReader {
Scanner reader;
public ConsoleReader() {
reader = new Scanner(System.in);
//reader.useDelimiter(System.getProperty("line.separator"));
}
public int readInt(String msg) {
int num = 0;
boolean loop = true;
while (loop) {
try {
System.out.println(msg);
num = reader.nextInt();
loop = false;
} catch (InputMismatchException e) {
System.out.println("Invalid value!");
}
}
return num;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
插入整数:
无效的值!
插入整数:
无效的值!
...
在Java中,我可以通过一个扫描一个字符串,然后我可以做的得心应手之类的东西,scanner.hasNext()或者scanner.nextInt(),scanner.nextDouble()等.
这允许一些非常干净的代码来解析包含数字行的字符串.
这是如何在C#中完成的?
如果你有一个字符串说:
"0 0 1 22 39 0 0 1 2 33 33"
Run Code Online (Sandbox Code Playgroud)
在Java中,我会将其传递给扫描仪并执行操作
while(scanner.hasNext())
myArray[i++] = scanner.nextInt();
Run Code Online (Sandbox Code Playgroud)
或者非常相似的东西.什么是C#'这样做的方法?
如果我有一个带读者的方法,我想在读卡器上使用扫描仪操作,如下所示:
Scanner scanner = new Scanner(reader);
while(scanner.hasNext()) {
//blah blah blah
}
Run Code Online (Sandbox Code Playgroud)
关闭是否安全scanner?文档说它"关闭了这个扫描仪",然后讨论关闭底层可读的.假设我不想关闭可读性,而是希望调用者reader在准备好时关闭.scanner在这里关闭是否安全?
我正在编写一个程序,询问该人的全名,然后接受该输入并将其反转(即John Doe - Doe,John).我开始尝试获取输入,但它只获得了第一个名字.
这是我的代码:
public static void processName(Scanner scanner) {
System.out.print("Please enter your full name: ");
String name = scanner.next();
System.out.print(name);
}
Run Code Online (Sandbox Code Playgroud) 当我通过扫描仪读取文件时,我的程序中出现了运行时异常.
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Day1.ReadFile.read(ReadFile.java:49)
at Day1.ParseTree.main(ParseTree.java:17)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
while((str=sc.nextLine())!=null){
i=0;
if(str.equals("Locations"))
{
size=4;
t=3;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Professions"))
{
size=3;
t=2;
str=sc.nextLine();
str=sc.nextLine();
}
if(str.equals("Individuals"))
{
size=4;
t=4;
str=sc.nextLine();
str=sc.nextLine();
}
int j=0;
String loc[]=new String[size];
while(j<size){
beg=0;
end=str.indexOf(',');
if(end!=-1){
tmp=str.substring(beg, end);
beg=end+2;
}
if(end==-1)
{
tmp=str.substring(beg);
}
if(beg<str.length())
str=str.substring(beg);
loc[i]=tmp;
i++;
if(i==size ){
if(t==3)
{
location.add(loc);
}
if(t==2)
{
profession.add(loc);
}
if(t==4)
{
individual.add(loc);
}
i=0;
}
j++;
System.out.print("\n");
}
Run Code Online (Sandbox Code Playgroud) 我有一个Scanner链接System.in.现在,在使用之后Scanner,我应该关闭它,因为它是不好的编码实践,让它打开.但是,如果我关闭Scanner,我也将关闭System.in!任何人都可以告诉我如何关闭Scanner没有关闭System.in(如果有任何方式).