Click here to Skip to main content
16,020,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
data is written in my file like this
Quote:
P1,19
P2,25


the code i tried to access age is this

Java
try (Scanner scanner = new Scanner((new FileReader("names-with re.txt")))) {
           scanner.useDelimiter(",");
           while (scanner.hasNextLine()) {
               String s = scanner.next();
               scanner.skip(scanner.delimiter());
               int num = scanner.nextInt();
           }
       }



but i get an `InputMismatchException` with line `int num = scanner.nextInt();`
but why is that? when I read the first string till `,` and put it inside String `s` and then skip the delimiter, isn't the next value an int?

What I have tried:

i tried some way's around but nothing worked.
Posted
Updated 7-Jul-19 2:12am

1 solution

Because you are specifically saying that your data is separated solely by commas, and it isn't. The data you show has two separators: comma and newline.
Try this:
Java
import java.util.*;

public class Main
    {
    public static void main (String[]args)
        {
        System.out.println ("Hello World");
        Scanner scanner = new Scanner ("P1,19\nP2,25");
        scanner.useDelimiter (",|\\n");
        while (scanner.hasNextLine ())
            {
            String s = scanner.next ();
            scanner.skip (scanner.delimiter ());
            int num = scanner.nextInt ();
            System.out.println (s);
            System.out.println (num);
            }
        }
    }
 
Share this answer
 
v2
Comments
hiwa doski 7-Jul-19 8:33am    
your code worked and printed them but at the end i still get an exception !
i wrote your code like this
try (Scanner scanner = new Scanner((new FileReader("names-with re.txt")))) {
            scanner.useDelimiter(",|\\n");
            while (scanner.hasNextLine()) {
                String s = scanner.next();
                scanner.skip(scanner.delimiter());
                int age = scanner.nextInt();
                System.out.println(s);
                System.out.println(age);
            }
        }

and i still get NoSuchElementException with line `String s = scanner.next();', why is that ?
OriginalGriff 7-Jul-19 8:45am    
Well, look at your data and find out! I don't have access to it...
hiwa doski 7-Jul-19 8:50am    
my data is just like i mentioned it's a text file containing
P1,19
P2,25
Dave Kreskowiak 7-Jul-19 9:21am    
Is there a blank line with only a newline as the last line in the file? I'd be willing to bet on "yes".

That would mean your file looks like this:
P1,19\nP2,25\n\n
hiwa doski 7-Jul-19 9:43am    
yeah, I think there is because there is a number 3 line that show's in my IDE which is blank, kudos to you for figuring it out and thanks for your help, but one last question what i can do with that to make it not throw exception I already put '\n' in delimiter so what else can be done about it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900