Java编程Scanner读取text文件问题?
text.txt文件格式如下:1.11.21.31.4lineone2.12.12.32.4linetwo...Java中Scanner类基于行与标记的混合处理程序Fil...
text.txt文件格式如下:
1.1 1.2 1.3 1.4 lineone
2.1 2.1 2.3 2.4 linetwo
...
Java中Scanner类基于行与标记的混合处理程序
File file = new File("C:/text.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String text = input.nextLine();
System.out.println(text);
Scanner data = new Scanner(text);
for (int i = 0; i < 4; i++) {
double vector = data.nextDouble();
System.out.println(vector);
}
System.out.println(data.next());
}
运行后在double vector = data.nextDouble();行抛出异常java.util.NoSuchElementException
请问高手,程序有什么问题?怎么解决?多谢了! 展开
1.1 1.2 1.3 1.4 lineone
2.1 2.1 2.3 2.4 linetwo
...
Java中Scanner类基于行与标记的混合处理程序
File file = new File("C:/text.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String text = input.nextLine();
System.out.println(text);
Scanner data = new Scanner(text);
for (int i = 0; i < 4; i++) {
double vector = data.nextDouble();
System.out.println(vector);
}
System.out.println(data.next());
}
运行后在double vector = data.nextDouble();行抛出异常java.util.NoSuchElementException
请问高手,程序有什么问题?怎么解决?多谢了! 展开
3个回答
展开全部
改成这样的:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Scan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("C:/text.txt");
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (input.hasNextLine()) {
String text = input.nextLine();
System.out.println(text);
Scanner data = new Scanner(text);
for (int i = 0; i < 4; i++) {
double vector = data.nextDouble();
System.out.println(vector);
}
System.out.println(data.next());
}
}
}
运行结果为:
1.1 1.2 1.3 1.4 lineone
1.1
1.2
1.3
1.4
lineone
2.1 2.1 2.3 2.4 linetwo
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Scan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("C:/text.txt");
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (input.hasNextLine()) {
String text = input.nextLine();
System.out.println(text);
Scanner data = new Scanner(text);
for (int i = 0; i < 4; i++) {
double vector = data.nextDouble();
System.out.println(vector);
}
System.out.println(data.next());
}
}
}
运行结果为:
1.1 1.2 1.3 1.4 lineone
1.1
1.2
1.3
1.4
lineone
2.1 2.1 2.3 2.4 linetwo
展开全部
NoSuchElementException表示元素类型不匹配,你检查下是不是输入的数据不是double类型的或者是在其他地方出现了类型错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
判断条件不对
Scanner data = new Scanner(text); 这个是干什么的????
下面的是可用的
input = new Scanner(file);
while (input.hasNext()) {
System.out.println(input.next());
}
Scanner data = new Scanner(text); 这个是干什么的????
下面的是可用的
input = new Scanner(file);
while (input.hasNext()) {
System.out.println(input.next());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询