Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8723601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:39:55+00:00 2026-06-13T07:39:55+00:00

Given a File and a Scanner object, File simpleFile = ranFi.getSelectedFile(); Scanner text =

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:39:55+00:00Added an answer on June 13, 2026 at 7:39 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public StormAnalysis(){ try { fScanner = new Scanner(new File(tracks1949to2010_epa.txt)); while(fScanner.hasNextLine()){ System.out.println(fScanner.nextLine()); } } catch
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Two general questions I'm wondering about both in the case for a given file(.js,
I'm trying to find the number of characters in a given text file. I've
Scanner fileScanner = new Scanner(new File(C:/Users/User1/Documents/ServerSettings.dat)); The code above cannot find the file specified.
Given: File f = new File(test.txt); this way, each time i create a reference
For any given file data size, I want to be able to resize (or
I want to check whether a given file exists in android sd card. I
I have to search a given file name (let say Keyword) in a directory
I am trying to get the public URL of a given file stored in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.