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

  • SEARCH
  • Home
  • 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 8773873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:24:57+00:00 2026-06-13T18:24:57+00:00

I’ve been doing a small project for class, it runs perfectly without problems but

  • 0

I’ve been doing a small project for class, it runs perfectly without problems but when pitted against the class’s auto testers it gives back 2 No line found errors. Asking the course’s staff they say it’s probably because I’m trying to scan a line when none exist, but I tried printing all my scans and didn’t discover anything like that.
That’s all the scans I have in my code:

Scanner sc = new Scanner(System.in);
    String sentence;
    int choice;

    System.out.println("Please enter a sentence:");
    sentence = sc.nextLine();

    printMenu(); // calls a function to print the menu.

    // gets the require action
    System.out.println("Choose option to execute:");
    choice = sc.nextInt();
    sc.nextLine();

(I tried with and without the last sc.nextLine)

static void replaceStr(String str)
{
    String oldWord, newWord;
    Scanner in = new Scanner(System.in);

    // get the strings
    System.out.println("String to replace: ");
    oldWord = in.nextLine();
    System.out.println("New String: ");
    newWord = in.nextLine();

    // replace
    str = str.replace(oldWord, newWord);
    System.out.println("The result is: " + str);
    in.close();
}
static void removeNextChars(String str)
{
    Scanner in = new Scanner(System.in);
    String remStr; // to store the string to replace
    String tmpStr = ""; //the string we are going to change.
    int i; // to store the location of indexStr

    // gets the index
    System.out.println("Enter a string: ");
    remStr = in.nextLine();
    i=str.indexOf(remStr);

    in.close(); // bye bye

    if (i < 0)
    {
        System.out.println("The result is: "+str);
        return;
    }

    // Build the new string without the unwanted chars.
    /* code that builds new string */

    str = tmpStr;
    System.out.println("The result is: "+str);
}

Any idea how a line can leak here?

  • 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-13T18:24:58+00:00Added an answer on June 13, 2026 at 6:24 pm

    Here is the problem. You are using in.close(); at multiple places(last statement in replaceStr method and around the middle in removeNextChars method). When you close the scnaner using close() method, it closes your InputStream (System.in) as well. That InputStream can’t be reopened with-in your program.

    public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**

    Any read attempts after the scanner close will result into exception NoSuchElementException.

    Please close your scanner only once, when your program is done.

    EDIT: Scanner Closing/usage:

    In yout main function:

       Scanner sc = new Scanner(System.in);
       ....
       .....
       replaceStr(Scanner sc, String str);
       .....
       ....
       removeNextChars(Scanner sc ,String str);
       ....
       ....
       //In the end
       sc.close();
    
    
    static void replaceStr(Scanner in, String str){
      //All the code without scanner instantiation and closing
      ...
    }
    
    static void removeNextChars(Scanner in, String str){
      //All the code without scanner instantiation and closing
      ...
    }
    

    You should be all good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I

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.