1

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");

} }

4 Answers 4

4
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

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers, this was the problem. Thanks for the help =]
1

Try this:

scan.nextLine().charAt(0) == 'Y'

Comments

1

When comparing Strings or anyother object for that matter you need to use the .equals(Object other) method. You can only use == with primatives ( boolean, int, double,...)

scan.nextLine().equals("Y");
//or
scan.next().equals("Y");

There is also an method to take the string to Uppercase that would allow the user to enter "y" or "Y"

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

Comments

0

You should be using the Equals method for Strings:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.