0

I'm making a simple program in Java, in which ask the user:

  • initialBalance, double
  • rate, integer
  • client, String

I have a problem: the first two inputs work, but I cannot make the third to function, the program seems to skip it.

This is the code:

import java.util.Scanner;
public class DoublingTime{
    public static void main(String[] args){
        Scanner console = new Scanner(System.in);

        System.out.print("Initial sum: ");
        final double initialBalance = console.nextDouble();

        System.out.print("Interest rate: ");
        final int rate = console.nextInt();

        System.out.print("Client name: ");
        final String client = console.nextLine();
        
        double balance = initialBalance;
        int year = 0;

        while(balance < 2 * initialBalance){
            balance = balance + balance * rate / 100;
            year++;
        }

        System.out.println("To double the investment of " + client + " (interest rate of " + rate + "%) " + year + " years are needed.");

        console.close();
    }
}

And this is what I get on my GUI:

Initial sum: 10000
Interest rate: 1
Client name: To double the investment of (interest rate of 1%) 70 years are needed.

What am I doing wrong?

Dada
  • 6,313
  • 7
  • 24
  • 43
  • 4
    When you use `console.nextInt();` or `console.nextDouble();` it only takes the next number, and does not consume the rest of the line. So before you attempt to get the next line with `client = console.nextLine();` you need to consume the rest of the previous line with a blank call `console.nextLine();` that assigns the input to nothing. – sorifiend Dec 21 '21 at 22:31
  • 1
    Use console.next() to read the next line from the console. – Minh Kieu Dec 21 '21 at 22:44

0 Answers0