0

I am unsure what to do as I am quite new to Java. I want to repeat this program again but it keeps giving me the error of my string being out of range. How do I approach this?

import java.util.Scanner;
import java.io.*;

public class LoanReport {

    public static void main(String[] args) throws IOException {
        double loanBalance;
        double interestRate;
        int loanYears;
        String answer;
        char again;
        // boolean again;

        Scanner keyboard = new Scanner(System.in);

// while(again)
// do{
        System.out.print("Enter the " + "loan amount.");
        loanBalance = keyboard.nextDouble();
        System.out.print("Enter the " + "annual interest rate.");
        interestRate = keyboard.nextDouble();
        System.out.print("Enter the " + "years of the loan.");
        loanYears = keyboard.nextInt();

        Amortization am = new Amortization(loanBalance, interestRate, loanYears);

        am.saveReport("LoanAmortization.txt");
        System.out.println("Report saved to " + "the file LoanAmortization.txt.");

        System.out.print("Would you like " + "to run another report? Enter Y for " + "yes or N for no: ");
//answer = keyboard.next(); 
        // again = answer.charAt(0);
//} 
//while(answer == 'Y' || answer  == 'y');
        answer = keyboard.nextLine();
        again = answer.charAt(0);
        if (again == 'N' || again == 'n') {
            System.exit(0);
        }
    }
}

I tried doing a boolean method but I'm unsure if I did that correctly.

Abra
  • 19,142
  • 7
  • 29
  • 41

1 Answers1

0

String out of range error occurs when you are trying to access a position in a string, in this case answer.charAt(0), that does not exist.

nextLine(): Advances this scanner past the current line and returns the input that was skipped. next(): Finds and returns the next complete token from this scanner. See more here

Try using:

answer = keyboard.next()

instead of:

answer = keyboard.nextLine();

Also, use answer.equals("N") instead of if (answer == "N"), the later one is comparing the reference of the value.

Mylonas K.
  • 389
  • 1
  • 3
  • 13
  • It runs but it does not repeat the program again when I put "Y" – ihateranch808 Dec 07 '22 at 08:13
  • Of course it doesn't, you have just told it what to do if they choose "N", which is the option for the program to stop. You need an else if statement or a while loop to keep the application going. – Mylonas K. Dec 07 '22 at 08:16
  • When doing the else if statement, I am slightly confused. I did if (answer == "Y" || answer == "y") { this is the part I am confused with, not sure how to loop) } else { system.exit(0) } – ihateranch808 Dec 07 '22 at 08:19
  • Put the entire thing in a while loop while(continue) where boolean continue = true initially, and it should keep going until the user hits 'N' and the application stops. Mind you this will keep going untill the user hits 'N' or 'n', any other input will make the app repeat. – Mylonas K. Dec 07 '22 at 08:29
  • Did it all work out in the end? Don't forget to use equals instead of == as mentioned in my answer as equals is the correct way to compare strings instead of references of values. – Mylonas K. Dec 07 '22 at 08:44