Code to read from a file and then putting it into a constructor won't work

I'm a college student and I have a project in java and I'm trying to read from files and putting them into a constructor. The file i'm trying to read from is in this form:

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 

I'm trying to read the tokens from the line token by token and put each of them in my constructor. Here is my code:

i know I have a lot of flows in writing my classes , that's because i only started learning java programming about 4 months ago , however what i'm trying to do is to read the first line of the file and separate each token in it i tried to inhance my code to lock like this , 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());            





       }

Can you help me solving his issue please!

this is the error i'm having Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907)

at 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 Result: 1 line 260 is

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


Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan are not valid integer.

1.First read String

String str = readLline.next();

2. Use Integer.parseInt() method to validate the integer input.

suppose

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

For the first line:

2 Sciense [mr ali hassan  14/4/1993 ] Ali  Hhassan 13234 12/3/1998 123 1234567891234 1234567891 engineer

scanner works as follows:

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

String Status = fileInput.next(); // ali 

It's already wrong here...


The InputMismatchException indicates that what is being read from your file doesn't match the data type you are trying to store it in. The error is at line 258 of your class (turn on line numbers in your editor). I suspect it is one of your int's, you are either trying to read in a String to an int, or you are overflowing an int (ie the number you are reading in is greater than MAX_INT).

On a side note you should use lower case names for your variable names. The way you have it written it is difficult to tell a variable name from a Class name.

Here is the JavaDoc for the exception:

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

链接地址: http://www.djcxy.com/p/17216.html

上一篇: Java:扫描器编译错误

下一篇: 从文件中读取代码然后将其放入构造函数中的代码将不起作用