I have the following program that works fine by itself. The problem arises when combined with other static methods that try to do the same thing. On some ide's (BlueJ) it works fine but on others (eclipse) it doesn't.
When it doesn't work it hangs on input = kboard.nextLine();
I think eclipse maybe a more robust ide. Anyway, how to close kboard without closing System.in for future use?
/**
* Suppose you want to capture input from the keyboard
* but don't know what the user will type in. You could
* get text, or a number or you could just get [enter].
*
* How can you capture the input?
*/
import java.util.Scanner;
public class Kboard
{
public static void main(String[] args){
boolean text = false,
number = false,
enter = false;
String input = "";
Scanner kboard = new Scanner(System.in);
while(!input.equals("**")){
input = kboard.nextLine();
Scanner kstring = new Scanner(input);
/**
* if length() of input = 0 we must have gotten [enter]
*/
/**
* notice the order of the else if statements
* what happens if you reverse them?
*/
if(input.length() == 0) enter = true;
else if(kstring.hasNextDouble()) number = true;
else if(kstring.hasNext()) text = true;
System.out.println("[enter] : " + enter + "\n" +
"text : " + text + "\n" +
"number : " + number);
/**
* we close kstring before we create a new one
*/
kstring.close();
text = false;
number = false;
enter = false;
}
/**
* we close kboard before we exit
*/
kboard.close();
}
}