There is no such elementary exception scanner

Scanner s = new Scanner(new File("src/mail_list"));    
while (s.hasNextLine()){
        String line1 = s.nextLine();
        if (line1.startsWith("Users")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) {
                String arr[] = line1.split(" ", 4);
                users.add(new User(arr[0],arr[1],arr[2],arr[3]));
            }
        }
        if (line1.startsWith("Lists")){
            line1 = s.nextLine();
            while (!(line1 = s.nextLine()).isEmpty()) { //exception here
                String arr1[] = line1.split(" ", 2);
                ArrayList<String> tmp = new ArrayList<String>();
                StringTokenizer st = new StringTokenizer(arr1[1]);
                while (st.hasMoreTokens()) {
                    tmp.add(st.nextToken());
                }
                list.add(new List((arr1[0]), tmp));
            }
        }
    }

/*-testfile-start*/
Keyword: Users
username1 Name1 Surname1 email1
username2 Name2 Surname2 email2

Keyword: Lists
list_name username1 username2 ...
/*-testfile-end*/

      

I am using the above code to sort things from the above test file template. This basically means that if I come across the keyword "Users", I have to add the specified user information.

I have noted in the code where the exception is increasing. Any ideas on how to counter this?

+3


source to share


5 answers


You call nextLine()

twice, but only check once hasNextLine()

.

String line1 = s.nextLine();
    if (line1.startsWith("Users")){
        line1 = s.nextLine();

      



The value you get is the next line, not knowing if there is one, which throws an exception if there isn't one.

+1


source


I found a silly solution for it. I just added the 'dummy' char 2 lines after the last line. and it works. This is not a perfect solution, but since the test file is not meant to be viewed by any1, I'll take it now ... Thanks to everyone who brainstormed with me in these 45 minutes.



+1


source


Do! (line1 = s.nextLine ())! = null, not empty as it cannot read an empty line.

0


source


From Scanner#nextLine

:

Throws:
 NoSuchElementException

- if the string is not found.

And you have this code:

while (s.hasNextLine()) {
    //checked if there line to read
    String line1 = s.nextLine();
    if (line1.startsWith("Users")) {
        //not checked if there line to read
        line1 = s.nextLine();
        //not checked here either
        while (!(line1 = s.nextLine()).isEmpty()) {
            String arr[] = line1.split(" ", 4);
            users.add(new User(arr[0],arr[1],arr[2],arr[3]));
        }
    }
    //similar in code below...
}

      

Be Scanner#nextLine

sure to check the string is correct before use . Handle exceptions accordingly.

0


source


enter image description here

As you can see, this method throws a NoSuchElementException when the string is not found.

if (line1.startsWith("Lists")){
        line1 = s.nextLine(); // <=============== ?
        while (!(line1 = s.nextLine()).isEmpty()) { //exception here

      

How do you know that you have more lines in your comment line too?

0


source







All Articles