0

I have this following java program which is working fine without while loop, but I want to run the execution until user pressed Q key from the keyboard.

So What condition should be put in while loop which breaks the loop?

import java.awt.event.KeyEvent;
import java.util.Scanner;
import static javafx.scene.input.KeyCode.Q;

public class BinaryToDecimal {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);        
        while(kbhit() != Q){
            System.out.print("Input first binary number: ");
            try{          
                String n = in.nextLine();
                System.out.println(Integer.parseInt(n,2));
            }
            catch(Exception e){
                System.out.println("Not a binary number");
            }
        }
    }
}

Any help would be great. Thank You.

4
  • I know kbhit() in C language, but don't know in java Commented Sep 24, 2017 at 7:29
  • Question is unclear until you show that method Commented Sep 24, 2017 at 7:31
  • So what exactly is your question? Do you want to know, how you can read a keyboard input? Commented Sep 24, 2017 at 7:31
  • what condition should be put in while loop which breaks the loop? Commented Sep 24, 2017 at 7:34

2 Answers 2

3

I don't think you can use KeyEvent within a console application as there's no keyboard listener defined.

Try a do-while loop to watch for an input of the letter q. And you should compare strings using equals method

    Scanner in = new Scanner(System.in);        
    String n;
    System.out.print("Input first binary number: ");
    do {
        try{          
            n = in.nextLine();
            // break early 
            if (n.equalsIgnoreCase("q")) break;
            System.out.println(Integer.parseInt(n,2));
        }
        catch(Exception e){
            System.out.println("Not a binary number");
        }
        // Prompt again 
        System.out.print("Input binary number: ");
    } while(!n.equalsIgnoreCase("q"));
Sign up to request clarification or add additional context in comments.

4 Comments

The output is looking same whether we put " if " statement in the try block. Let me know if it reduces the unnecessary overhead or not?
Not sure what you mean. Obviously you'd like to prevent parsing letter q as an integer
In try block, "if (n.equalsIgnoreCase("q")) break;" Will it reduces the execution time or performance time? In case if user's input is "q" in the beginning.
Of course it adds execution time, it's another statement. But it's negligible. Why do you care about performance of such simple code?
0

What about this?

public class BinaryToDecimal {
    public static void main(String[] args) {
        System.out.print("Input first binary number: ");
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        while(!"q".equalsIgnoreCase(line.trim())){
            try{
                System.out.println(Integer.parseInt(line,2));
                System.out.print("Input next binary number: ");          
                line = in.nextLine();
            }
            catch(Exception e){
                System.out.println("Not a binary number");
            }
        }
    }
}

2 Comments

Well, you are prompting for input before "Input first binary number", which might not be obvious
thanks for pointing it out! fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.