3

我是一名大学生,我有一个 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 编程,但是我想要做的是读取文件的第一行并将其中的每个标记分开我试图增强我的代码以像这样锁定,File F= new File ("Book.txt");

       Scanner fileInput = new Scanner (F);
       while (fileInput.hasNextLine()){
       String Line = fileInput.nextLine();      
       Scanner readLline = new Scanner(Line);    

       while(readLline.hasNext()){
       //reads line by line
       readBook.setNumOfAuthor(readLline.nextInt());
       readBook.SetAplicationTitle(fileInput.next(Line));
       String GetRedOf = fileInput.next();    
       ba.setStatus(fileInput.next()); 
       ba.setFirstName(fileInput.next()) ;
       ba.setLastName(fileInput.next());
       Adate.setDay(fileInput.nextInt());
       String GetRedOf3 = fileInput.next();
       Adate.setMonth(fileInput.nextInt());
       String GetRedOf4 = fileInput.next();
       Adate.setYear(fileInput.nextInt() ) ;
      //  String comma = fileInput.next();
       String GetRedOf2= fileInput.next();
       bb.setName(fileInput.next()); 
       bb.setAdress(fileInput.next());
       bb.setphneNumber(fileInput.next());
       publicationDate.setDay(fileInput.nextInt())  ;
       String getred = fileInput.next();
       publicationDate.setMonth(fileInput.nextInt()); 
       String getred1 = fileInput.next();
       publicationDate.setYear(fileInput.nextInt()) ;
       readBook.SetNumOfPUblication(fileInput.nextInt()); 
       readBook.setIsbn13(fileInput.next()) ;  
       readBook.setIsbn13(fileInput.next());  
       readBook.SetCatagory(fileInput.next());            





       }

你能帮我解决他的问题吗!

这是我在 java.util.Scanner.throwFor(Scanner.java:907) 的线程“main”java.util.NoSuchElementException 中遇到异常的错误

在 java.util.Scanner.next(Scanner.java:1530)

at java.util.Scanner.next(Scanner.java:1463)

at TestPublications.ReadBook(TestPublications.java:260)

at TestPublications.main(TestPublications.java:232)

Java 结果:1 行 260 是

readBook.SetAplicationTitle(fileInput.next(Line));

4

4 回答 4

0

对于第一行:

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 

这里已经错了……

于 2014-04-25T15:26:55.263 回答
0
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");
}
于 2014-04-25T15:27:26.507 回答
0

InputMismatchException 表示从文件中读取的内容与您尝试存储的数据类型不匹配。错误位于类的第 258 行(在编辑器中打开行号)。我怀疑它是您的 int 之一,您要么尝试将 String 读入 int,要么溢出 int(即您正在读取的数字大于 MAX_INT)。

附带说明一下,您应该使用小写的名称作为变量名。您编写它的方式很难从类名中区分变量名。

这是异常的 JavaDoc:

http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html

于 2014-04-25T15:30:03.973 回答
0

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

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:2 2: 科学 3: 阿里哈桑先生 14/4/1993

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

于 2014-04-25T15:54:04.353 回答