Search code examples
javafilejava.util.scannerinputmismatchexception

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));


Solution

  • 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");
    }