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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:47:39+00:00 2026-05-27T08:47:39+00:00

I am trying to make a simple calculator program where a user can opt

  • 0

I am trying to make a simple calculator program where a user can opt to do an operation and enter the numbers as long as he/she wishes to.

I just have problem because whenever I would reach inside my loop and ask the user if he/she want to continue and whenever I would run the program, I would have an “Exception in thread “main” java.lang.NullPointerException”

Scanner myInput=new Scanner(System.in);

System.out.print("Do you have numbers to compute?");
ans=myInput.findInLine(".").charAt(0);

while ((ans=='Y')||(ans=='y'))
{
    //get the numbers
    //provide the menu
    //get the user's choice

    switch (calc)
    {
        case 1: out.println("Sum is: " +(num1+num2)); break;
        case 2: out.println("Difference is: " +(num1-num2)); break;
        case 3: out.println("Product is: " +(num1*num2)); break;
        case 4: out.println("Quotient is: " +(num1/num2)); break;
        case 5: out.println("Modulo is is: " +(num1%num2)); break;
        case 6: out.println("Sum is: " +(num1+num2));
                    out.println("Difference is: " +(num1-num2));
                    out.println("Product is: " +(num1*num2));
                    out.println("Quotient is: " +(num1/num2));
                    out.println("Modulo is is: " +(num1%num2)); break;
                    default: out.println("Invalid."); break;
    }
       out.println("Compute another?");
       ans=myInput.findInLine(".").charAt(0);
}

May I humbly ask what can I do with this program so that it will ask again for the user’s input whether to continue or not? Thanks in advance for your help.

  • 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-05-27T08:47:40+00:00Added an answer on May 27, 2026 at 8:47 am

    The NullPointerException is being thrown as Scanner.findInLine() can return null if it does not find the requested String/Pattern. The code invokes the method charAt(0) on the null String. Changing the code to the following will resolve this:

    Scanner myInput=new Scanner(System.in);
    char ans;
    
    System.out.print("Do you have numbers to compute?");
    ans=myInput.findInLine(".").charAt(0);
    
    while ((ans=='Y')||(ans=='y'))
    {
        // Code omitted
    
        System.out.print("Compute another?");
        myInput.nextLine();
        String s = myInput.findInLine(".");
        ans = (null == s) ? 'n' : s.charAt(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple calculator application in cocoa. The program hangs when
Just trying to make a simple calculator in javascript. When I try to run
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I'm trying to make a simple GUI calculator in Python with Tkinter. However the
I am trying to make a simple calculator. I am getting blue line under
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make some simple XML with Java and org.w3c.dom, but I got
I'm trying to make a simple drawing app in c++, but i'm having trouble
I'm trying to make a simple php script to find all src attributes from
I am trying to make a simple script to automate and log synchronization via

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.