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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:22:49+00:00 2026-06-10T12:22:49+00:00

I’m having an issue with the code below. It’s all working fine, except I

  • 0

I’m having an issue with the code below. It’s all working fine, except I want the program to restart if the user types in “Y” at the end, and end if anything else is pressed.

However, whenever I type anything at the “Restart Calculator” prompt, it will stop running, regardless of whether I type in “Y” or “N”. Validation with the Y/N is not too important here, I just want it to restart if Y is typed and end if anything else is typed.

Apologies for the noob code, Java beginner here.

import java.util.Scanner;
import java.text.*;

public class Savings {

public static void main(String[] args)
{

//Imports scanner, to read user's input
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);

do {
    //Asks for and receives user's initial deposit
    int initial_Deposit;
    do {
        System.out.print("Enter initial deposit in dollars (Between $1 - $50000: ");
        while (!scan.hasNextInt()) {
            System.out.println("Please enter a valid number between '1-50000'");
            scan.next();
        }
        initial_Deposit = scan.nextInt();
    } while (initial_Deposit <= 0 || initial_Deposit >= 50001);

    //Asks for and receives user's interest rate
    double interest_Rate;
    do {
        System.out.print("Enter interest rate as a percentage between '0.1-100.0' (e.g. 4.0):");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter interest rate as a percentage between '0.1-100.0' (e.g. 4.0):");
            scan.next();
        }
        interest_Rate = scan.nextDouble();
    } while (interest_Rate <= 0.0 || interest_Rate >= 100.1);

    //Asks for and receives user's monthly deposit
    int monthly_Deposit;
    do {
        System.out.print("Enter monthly deposit in dollars between '$1 - $5000: ");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter monthly deposit in dollars between '$1 - $5000: ");
            scan.next();
        }
        monthly_Deposit = scan.nextInt();
    } while (monthly_Deposit <= 0 || monthly_Deposit >= 5001);

    //Asks for and receives user's investment duration  
    int monthly_Duration;
    do {
        System.out.print("Enter investment duration (Between 1 and 12): ");
        while (!scan.hasNextDouble()) {
            System.out.println("Enter investment duration (Between 1 and 12): ");
            scan.next();
        }
        monthly_Duration = scan.nextInt();
    } while (monthly_Duration <= 0 || monthly_Duration >= 13);

    //Asks for and receives user's first name
    String first_Name;
    System.out.print("Enter first name: ");
    first_Name = input.next();

    //Asks for and receives user's surname
    String last_Name;
    System.out.print("Enter surname: ");
    last_Name = input.next();

    //Formats first name to only first letter
    char firstLetter = first_Name.charAt(0);

    //Changes name to correct format
    String formatted_Name;
    formatted_Name = "Savings growth over the next six months for " + last_Name + ", " + firstLetter;
    System.out.println(formatted_Name);

    //Calculates the first balance
    double balanceCurrent;
    balanceCurrent = initial_Deposit + monthly_Deposit;

    //Prepares to format currency
    DecimalFormat df = new DecimalFormat("#.##");

    //Defining variables
    double balanceNew;
    double interestEarned;

    //Defining counter for while loop
    int counter;
    counter = monthly_Duration;
    int month_Counter;
    month_Counter = 1;

    //While loop to calculate savings
    while (counter > 0) {
        balanceNew = balanceCurrent + (balanceCurrent *((interest_Rate /12)/100));
        interestEarned = balanceCurrent *((interest_Rate /12)/100);
        balanceCurrent = balanceNew + monthly_Deposit;
        System.out.println("Balance after month " + month_Counter + ": $" + df.format((balanceNew)));
        System.out.println("Interest earned for this month: $" + df.format(interestEarned));
        counter = counter - 1;
        month_Counter = month_Counter + 1;

    }
    //Formats data into a table
    balanceCurrent = initial_Deposit + monthly_Deposit; 
    counter = monthly_Duration;
    int month;
    month = 0;
    String dollarSign = "$";

    String stringHeadingOne = "Month";
    String stringHeadingTwo = "New Balance";
    String stringHeadingThree = "Interest Earned";
    String dividerOne = "-----     -----------     ---------------";
    System.out.println("");
    System.out.printf("%-9s %s %19s \n", stringHeadingOne, stringHeadingTwo, stringHeadingThree);
    System.out.println(dividerOne);

    while (counter > 0) {
        balanceNew = balanceCurrent + (balanceCurrent *((interest_Rate /12)/100));
        interestEarned = balanceCurrent *((interest_Rate /12)/100);
        balanceCurrent = balanceNew + monthly_Deposit;
        month = month + 1;
        System.out.printf("%-11s %s %s %13s %s \n", month, dollarSign, df.format((balanceNew)), dollarSign, df.format(interestEarned));
        counter = counter - 1;
    }
System.out.print("Restart Calculator? Y/N);");
} while (scan.next() == "Y");

}
}

  • 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-10T12:22:50+00:00Added an answer on June 10, 2026 at 12:22 pm
    while (scan.next() == "Y");      // Is checking for reference equality
    

    When doing object comparisons in Java, use equals()

    while (scan.next().equals("Y"));
    

    Or, as the previous answer pointed out you can compare characters with the == operator

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.