OcelotXL
OcelotXL

Reputation: 158

File/Scanner simple parsing issues

Given a File and a Scanner object,

File simpleFile = ranFi.getSelectedFile();
Scanner text = new Scanner(simpleFile);

and these two commonplace statements:

    while(text.hasNext())
    {
        String currentLine = text.nextLine();

I'm trying to use Scanner/String class logical statements in a single if-statement clause which reads first line of file under a given matching regular expressions, such as:

String fp100 = "[S][:][A-Ze0-1]";
String fp200 = "[S][:][A-Z0-1][A-Z0-1]";
//other regexes…

and then invoke the appropriate Scanner/String class methods in same if-statement clause to read to second and onward/acceptable lines. I've read javadoc up and down but haven't figured out yet. Using currentLine.matches(regex) and text.nextLine().matches(regex), this code compiled,

    if(currentLine.matches(fp100)||currentLine.matches(fp200)||
       currentLine.matches(fp300) && text.nextLine().matches(fp100)||
       text.nextLine().matches(fp101) || text.nextLine().matches(fp200)||
       text.nextLine().matches(fp201) || text.nextLine().matches(fp300)||
       text.nextLine().matches(fp301))
    {

but throws an No Such Element Exception immediately. What am I doing wrong? Thank you in advance for your time. EDIT: I've included the stack trace, but removed the source code since this is project related.

stack trace

Upvotes: 0

Views: 152

Answers (2)

Yogendra Singh
Yogendra Singh

Reputation: 34367

I see two problems:

  1. When you perform the if condition, text.nextLine() may not be available.

  2. if you mean to say, execute the if when any of the currentLine Matches + any of the nextLine match as true then wrap || arguments in a brace as:

    if((currentLine.matches(fp100)||currentLine.matches(fp200)||
       currentLine.matches(fp300)) && 
       (text.nextLine().matches(fp100)||
        text.nextLine().matches(fp101) || text.nextLine().matches(fp200)||
        text.nextLine().matches(fp201) || text.nextLine().matches(fp300)||
        text.nextLine().matches(fp301)))
    

I think you wanted to write your while loop something like this:

        while(text.hasNextLine()){
           String currentLine = text.nextLine();
           String nextLine = "";
           if(text.hasNextLine())[
               nextLine  = text.nextLine();
           }

           /**ACC conditions*/
           if((currentLine.matches(fp100)||currentLine.matches(fp200)
                || currentLine.matches(fp300)) 
                && (nextLine.matches(fp100)|| nextLine.matches(fp101) 
                     || nextLine.matches(fp200)
                     || nextLine.matches(fp201) || nextLine.matches(fp300)
                     || nextLine.matches(fp301)) {
                                //current line is OK
                                System.out.println(currentLine);
                                output.write(currentLine);
                                output.write("\n");
                                abc1List.add(currentLine);
                                lineOK++;               

                                //next line is OK
                                System.out.println(nextLine);
                                output.write(nextLine);
                                output.write("\n");
                                abc1List.add(nextLine);
                                // <-- not sure if you want OK as 1 or 2 here 
                                lineOK++;           
           } /**REJ conditions*/
           else if(!currentLine.matches(fp100)||!currentLine.matches(fp101)||
                  !currentLine.matches(fp200)||!currentLine.matches(fp201)||
                  !currentLine.matches(fp300)||!currentLine.matches(fp301)){   
                        System.out.println("invalid cfg; terminating....");
                   System.exit(0);
           }
       }//end of while

Upvotes: 1

Juvanis
Juvanis

Reputation: 25950

Your while loop should start with while(text.hasNextLine()) if you are using text.nextLine().matches(regex) inside the loop. Be careful. If text.hasNext() evaluates to true, it doesn't mean that text.nextLine() will be non-null.

Upvotes: 1

Related Questions