system.in

System.in points to JtextArea and using Scanner with it causes application to hang

戏子无情 提交于 2019-12-14 03:26:48
问题 I've got a JFrame that contains a single JPanel that contains a single JTextArea. I've successfully managed to point System.out to the JTextArea, but when I try to use Scanner(System.in) to parse input from the JTextArea, it doesn't even seem to load anything. As in, when I build and run the application, nothing happens, no frame is presented. Here is my code: /** * Create the frame. */ public TerminalForm() { setTitle("Terminal"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds

Why does the string inside println method show twice?

試著忘記壹切 提交于 2019-12-13 07:51:01
问题 In the following code why does the string inside println method shows twice.What should I do to show the message once per iteration package practicejava; public class Query { public static void main(String[] args) throws java.io.IOException { System.out.println("Guess a capital letter Character"); while ((char) System.in.read() != 'S') { System.out.println("wrong.guess again to finish the program"); } } } 回答1: When a user writes in console characters, to confirm fact that his input is ready

How to close a scanner without closing the underlying System.in?

邮差的信 提交于 2019-12-13 01:30:48
问题 If I close one scanner object and make a new one and try to read some more input, I'm getting the NoSuchElementException exception. My code works fine but it gives me a warning if I don't close the scanner. However, if I close it to get rid of the warning, I also close System.in ... How do I avoid this? Also, are there any consequences of not closing the scanner? EDIT: Here's my code: This is the NameAddressExists() method: public void NameAddressExists() { System.out.println("Enter name");

How to Map System.out and System.in to SWT controls

浪尽此生 提交于 2019-12-11 10:01:31
问题 I am working on on a tool to connect to a remote server using jcraft.JSch (ssh to Unix server) and return the output and display its output. The script works fine when channel input outputs are System.in and System.out . But when I try with SWT controls, it is not able to display the exact output I am getting with System.out read from SWT Text control emulating System.in public void create() { dialogShell = new Shell(dialogShell, SWT.PRIMARY_MODAL | SWT.SHEET | SWT.RESIZE); GridLayout

Setting System.in to read from JTextField

为君一笑 提交于 2019-12-11 02:24:17
问题 I'm looking for direction on how to replace System.in with an InputStream that reads directly from a JTextField . So far my approach has pretty much been trial and error. I currently have; JTextField input = new JTextField(); System.setIn(new InputStream() { int ptr = 0; @Override public int read() throws IOException { int c; try { c = input.getText().charAt(ptr); } catch (IndexOutOfBoundsException ioob) { return 0; } ptr++; return c; } }); This yields an NoSuchElementException as in attempts

Docker Java Application failing at obtaining input from console

一世执手 提交于 2019-12-10 20:37:28
问题 I am trying to create a docker image for my java application. At startup this application needs to be given a password (currently via console). I tried several methods of obtaining input however they have all failed. Is this a limitation of docker and if so is there a workaround? For this snippet: Console console = System.console(); if(console == null){ System.out.println("console is null!!"); } else { System.out.println("Input password: "); char[] password = console.readPassword("Pass: "); }

basic reading input from user in java

南笙酒味 提交于 2019-12-08 15:13:52
问题 I want to read a character and store it into the char[] array and here is my method called getaline public static int getaline(char message[], int maxlength) { int index = 0; while (message[index] != '\n') { message[index] = fgetc(System.out); index++; } index++; } and my fgetc method: public static int fgetc(InputStream stream) and this method should returns a character from the input stream. But i keep getting an error message when i compile: error: possible loss of precision message[index]

java: console application main thread spawns a key listener thread

…衆ロ難τιáo~ 提交于 2019-12-08 13:09:24
问题 I wrote a tool, which is performing some steps 1..n Some of the steps require user interaction (reading from System.in) Other steps loop until some condition is fulfilled or the user pressed some key (When the user pressed the key the loop should end and the main should go to the next step) What I did for those steps, that provide a key loop interruption is to spawn a thread which reads from System.in -> this thread then interrupts the step, if key was pressed. This works fine, but when the

How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

↘锁芯ラ 提交于 2019-12-06 09:46:50
Here's the code: package testpack; import java.io.*; public class InputStreamReadDemo { private void readByte() throws IOException { System.out.print("Enter the byte of data that you want to be read: "); int a = System.in.read(); System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+"."); // tried writing System.in.close(); and System.in.reset(); } private void readLine() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a line

What is the type of System.out in Java?

让人想犯罪 __ 提交于 2019-12-04 09:15:02
I am just a newbie in Java. I was wondering the way System.out.println() is used. Out is a static field inside System class. The type of out is PrintStream . But when I saw the constructor of PrintStream class, it takes a parameter of type OutputStream and as far as I know we cannot create the object of an abstract class. In that case we must pass some subclass's object to the constructor of PrintStream . What is that class? Same is the System.in . It is also InputStream 's reference but what is the type of object it points to as the InputStream is abstract? PrintStream wraps