4

For example, if the user enters 7 * 4 I want it to output 7 * 4 = 28

Instead of

7* 4
=28

I've been looking for a couple hours and haven't found anything. Thanks for any help in advance.

public class RecursiveMultiplication 
{
public static void main(String[] args)
{
    Console console = System.console();
    String input[] = new String[2];
    String multiplicand, multiplier;
    int product;

    while(true)
    {
        input = console.readLine("?> ").split("\\D+");
        multiplicand = input[0];
        multiplier = input[1];
        product = multiply(Integer.parseInt(multiplicand), Integer.parseInt(multiplier)); 

        console.printf(" = %d", product);

    }
}

public static int multiply(int multiplicand, int multiplier){
    if(multiplier == 0)
        return 0;
    if(multiplier % 2 == 0)
        return multiplicand + multiplicand + multiply(multiplicand, multiplier - 2);
    return multiplicand + multiply(multiplicand, --multiplier);
}

}

Lightyear Buzz
  • 796
  • 5
  • 25
  • It has nothing to do with Java. The terminal is taking user input and displaying it. – Brian Roach Jan 24 '12 at 08:56
  • 1
    Have a look at @chris-w-rea answer to: http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it – xpapad Jan 24 '12 at 09:00

1 Answers1

4

This is a limitation of the console your Java program is running on. It is not easy to get around, but you can take a look at the Java Curses Library. Personally, I don't think it is worth the hassle. It would be easier to write a Java Swing GUI instead.

dogbane
  • 242,394
  • 72
  • 372
  • 395