[英]Code to read from a file and then putting it into a constructor won't work
我是一名大學生,我有一個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(線));
對於第一行:
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:
http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html
我建議使用正則表達式並從那里提取數據。 也許是這樣的:
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:Sciense 3:am hassan先生14/4/1993
將正則表達式擴展到整行,你就完成了:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.