Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件读取并将其放入构造函数的代码赢得';行不通_Java_File_Java.util.scanner_Inputmismatchexception - Fatal编程技术网

Java 从文件读取并将其放入构造函数的代码赢得';行不通

Java 从文件读取并将其放入构造函数的代码赢得';行不通,java,file,java.util.scanner,inputmismatchexception,Java,File,Java.util.scanner,Inputmismatchexception,我是一名大学生,我有一个java项目,我试图从文件中读取并将其放入构造函数中。我试图读取的文件格式如下: 2 Sciense [mr ali hassan 14/4/1993 ] Ali Hhassan 13234 12/3/1998 123 1234567891234 1234567891 engineer 2 Sciense [mr ali hassan 14/4/1993 ] Ali Hhassan 13234 12/3/1998 123 1234567891234 1234567

我是一名大学生,我有一个java项目,我试图从文件中读取并将其放入构造函数中。我试图读取的文件格式如下:

2 Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan 13234 12/3/1998 123 1234567891234 1234567891 engineer
2 Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan 13234 12/3/1998 123 1234567891234 1234567891 null
.
.
.
etc 
我试图从行令牌中逐个读取令牌,并将它们放入我的构造函数中。这是我的密码:


我知道我在写我的类时有很多流程,这是因为我大约4个月前才开始学习java编程,但是我试图做的是读取文件的第一行并分离其中的每个标记,我试图通过我的代码像这样锁定, 文件F=新文件(“Book.txt”)

你能帮我解决他的问题吗

这就是我犯的错误 线程“main”java.util.NoSuchElementException中出现异常 位于java.util.Scanner.throwFor(Scanner.java:907)

下一步(Scanner.java:1530)

Java结果:1 第260行是

SetAplicationTitle(fileInput.next(行))

对于第一行:

2 Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan 13234 12/3/1998 123 1234567891234 1234567891 engineer
扫描仪的工作原理如下:

int numofaouthers = fileInput.nextInt(); // 2
String SetAplicationTitle =fileInput.next(); // Sciense 
String GetRedOf = fileInput.next(); // [mr

String Status = fileInput.next(); // ali 
这里已经错了

Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan are not valid integer.
1.第一次读取字符串

String str = readLline.next();
2。使用Integer.parseInt()方法验证整数输入。

假设

try{
    Integer.parseInt(ste);
}
catch(NumberFormatException ex){
    System.out.println("Its not a valid Integer");
}

InputMismatchException表示从文件中读取的内容与试图存储该文件的数据类型不匹配。错误出现在类的第258行(在编辑器中打开行号)。我怀疑这是你的一个int,你要么试图从字符串中读入一个int,要么溢出了一个int(即,你读入的数字大于MAX_int)

另一方面,您应该使用小写的变量名。按照编写方式,很难区分变量名和类名

以下是异常的JavaDoc:


我建议使用正则表达式并从中提取数据。也许是这样的:

BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

String regex = "([0-9]+) ([a-zA-Z]+) \\[(.+)\\].+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher;

String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
    matcher = pattern.matcher(line);
    if (matcher.find()) {
        System.out.printf("1:%s 2:%s 3:%s", matcher.group(1),
                matcher.group(2), matcher.group(3));
    }
    break;
}
该示例匹配3个组:

1:22:Sciense 3:ali hassan先生1993年4月14日


将正则表达式扩展到整行,您就完成了:-)

强烈推荐您1。将文件的一行读取为字符串和2。分解字符串,而不是试图聪明地假设。下一步是获取所需的数据。它还可以让您正确地验证所有输入,其中一行是TestPublications.java:258?这应该很容易调试。只需在TestPublications.java:258上设置一个断点。异常意味着您试图读取的是非整数的整数。我强烈建议您逐行读取,以使用正则表达式提取数据!我知道我在写我的类时有很多流程,这是因为我大约4个月前才开始学习java编程,但是我试图做的是读取文件的第一行并分离其中的每个标记。我试图通过我的代码锁定,就像这样,我有一个无参数构造函数,我的方法从文件中读取并将每个标记指定给构造函数的一个属性,您是否有其他方法可以用于解决此问题,谢谢您line
Scanner fileInput=new Scanner(F)
创建了一个文件名为
F
的扫描仪,不是吗?为什么此时不能使用
BufferedReader
try{
    Integer.parseInt(ste);
}
catch(NumberFormatException ex){
    System.out.println("Its not a valid Integer");
}
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

String regex = "([0-9]+) ([a-zA-Z]+) \\[(.+)\\].+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher;

String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
    matcher = pattern.matcher(line);
    if (matcher.find()) {
        System.out.printf("1:%s 2:%s 3:%s", matcher.group(1),
                matcher.group(2), matcher.group(3));
    }
    break;
}