1

I'm learning Java and at this moment I try simple tasks to get used to with Java synthax.

Today I wrote a simple calculator and one of task goal was to finish program when user press enter key. So this is my problem, my code solution work with other keys, for example 'y'

code with 'y' key(works properly):

import java.util.Scanner;

public class SimpleCalculator {

public static void main(String[] args) {

    System.out.println("Program 'Simple Calculator'");
    boolean log = true; 
    Scanner in = new Scanner(System.in);

    while (log == true) {

        double result = 0;
        double a, b = 0;
        System.out.println("Enter the first number");
        a = in.nextDouble();
        char sign;
        String temp;
        boolean log2 = true;

        while (log2 == true) {
            System.out.println("Enter the algebraic operation sign ('+' ,'-' ,'*' or '/' )");
            temp = in.next();
            sign = temp.charAt(0);

            if (sign == '+' || sign == '-' || sign == '*' || sign == '/') {
                log2 = false;
                switch (sign) {
                case '+':
                    System.out.println("Enter the 2nd number ");
                    b = in.nextDouble();
                    result = a + b;
                    break;
                case '-':
                    System.out.println("Enter the 2nd number ");
                    b = in.nextDouble();
                    result = a - b;
                    break;
                case '*':
                    System.out.println("Enter the 2nd number ");
                    b = in.nextDouble();
                    result = a * b;
                    break;
                case '/':
                    while (b == 0) {
                        System.out.println("Enter the 2nd number ");
                        b = in.nextDouble();
                        if (b != 0)
                            result = a / b;
                        else
                            System.out.println("Division by 0 is undefined for real nubers, enter 2nd number again");
                    }
                    break;

                }

            } else {
                System.out.println("wrong operation sign, enter it once again");
            }
            if (log2 == false)
                System.out.println("Result: " + result);

        }
        System.out.println("Press y for exit the program(or any other to use it once again)"); 

        temp = in.next();
        if (temp.equals("y")) {
            System.out.println("y Key pressed.");
            log = false;
        }

    }
    in.close();
    System.out.println("End of work");

}

}

I tried two variations of last if to end work, when Enter is pressed

temp = in.next();
if (temp.equals("")) {
System.out.println("Enter Key pressed.");
log = false;}

and the second

temp =in.next();
        sign = temp.charAt(0);
        if (sign == 13 )    //carriage return character code
            log = false; 

but both are not good in case of enter key. It would be great if somebody tell me why this two variations of if don't work properly with enter key and what is the best way to end program with end key.

Thank You :)

ChGol
  • 51
  • 7
  • You should recheck what `next()` really does and ***when*** it does that. Then use `nextLine()` and read http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Tom Sep 27 '16 at 16:07
  • 1
    Possible duplicate of [Java using scanner enter key pressed](http://stackoverflow.com/questions/18281543/java-using-scanner-enter-key-pressed) – Young Emil Sep 27 '16 at 16:07
  • 1
    Possible duplicate of [How to recognize carriage return using java scanner](http://stackoverflow.com/questions/18859224/how-to-recognize-carriage-return-using-java-scanner) – Tim Biegeleisen Sep 27 '16 at 16:08
  • In the console, the enter key is recognized with the `\n` character. If you are performing `event handling`, then you can probably get the `key code` of the enter key as `13`. – progyammer Sep 27 '16 at 16:19
  • 1
    @Tom Thanks, blank in.nextLine() and then temp = in.nextLine() solve my problem. – ChGol Sep 27 '16 at 18:19
  • @progy_rock thanks I'll read about event handling – ChGol Sep 27 '16 at 18:19

0 Answers0