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 8485527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:40:33+00:00 2026-06-10T20:40:33+00:00

So my first assignment involves making a simple question and answer program. The user

  • 0

So my first assignment involves making a simple question and answer program. The user asks a question, and I generate an answer. I’ve never done java before. Here is my input class:

//msg is the msg I output (answer to their question).
//Function returns inputStr which is the question the user asks.
public String getInput(String msg) {
    System.out.println(msg);
    Scanner theInput = new Scanner(System.in);
    String inputStr = theInput.nextLine(); //ERROR HERE ON 2nd ITERATION!
    theInput.close();
    if (inputStr.equals("exit")) {
        System.out.println("GoodBye!"); 
        System.exit(0);
    }
    return inputStr;
}

The function that calls this in the while loop is as follows:

    //inputSource is an object that has the getInput method. It is an argument for this function.
    String userQuestion = inputSource.getInput(firstLine);
    String initMsg = processMessage(userQuestion);
    while(!initMsg.equalsIgnoreCase("GoodBye")){
        userQuestion = inputSource.getInput(initMsg);
        //Doesn't get to here.
        initMsg = processMessage(userQuestion);
    }
    System.out.println(initMsg);

Error:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)

So basically, what happens is that it asks a question once, and then it gives back an answer once, but when it enters the while loop, it gets stuck at the indicated point.
Little help. Thank you.

  • 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-10T20:40:34+00:00Added an answer on June 10, 2026 at 8:40 pm

    One thing that I noticed: you should probably not call close() on the scanner. You’re closing the underlying input stream (standard input), according to the JavaDocs.

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

Sidebar

Related Questions

I'm working on my first programming assignment for Java and I had another question.
I have my first professional assignment of making a website in which a photographer's
The assignment at my first year uni computing course says my program should read
I am currently completing my first java course and assignment based on a random
Assignment: Write a C++ program to read the menu information from menu.txt. First letter
I am nearly finished with my first assignment but the last question has me
In my assembly language class, our first assignment was to write a program to
First, I am new to programming and this is my first major assignment in
So I finished my first C++ programming assignment and received my grade. But according
First of all, this is a homework assignment. I'm trying to make some bubbleSort

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.