Question

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

Was it helpful?

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

OTHER TIPS

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 (i.e. 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

I would recommend using regular expressions and extract data from there. Maybe something like this:

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

That example matches 3 groups:

1:2 2:Sciense 3:mr ali hassan 14/4/1993

Extend the regex to the whole line and you're done :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top