How do I get input from System.in (Java) in Netbeans?
Asked Answered
R

6

5

I have a small Java test app in Netbeans where the main() class reads input from System.in. How can I open a window into which I can type input? (I am using NB 6.7.1 on Windows 7).

Racer answered 31/10, 2009 at 9:20 Comment(0)
A
12

It may not be obvious but in Netbeans the Output tab at the bottom also takes input if your main thread is waiting for input. Just type under the last output line and hit enter. In other words, the Output tab is the same as a console window.

Asserted answered 31/10, 2009 at 9:40 Comment(1)
Thanks - that is exactly what I was after. You are right, it is certainly not obvious to type input into the 'Output' console!Racer
L
3

I'm pretty confident the following worked in NB 6.5 Just type into the output window which happens to accept input

InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(inputStreamReader);
System.out.println("Type name:");
String name = reader.readLine();
System.out.println("Hello "+name);
Liveried answered 31/10, 2009 at 9:42 Comment(0)
Z
1

In Eclipse, you can just type in your console window. I suppose Netbeans would have a similar option.

Zenaidazenana answered 31/10, 2009 at 9:41 Comment(0)
I
0

If you just want a small window to type some input into, then the easiest way is to use JOptionPane. For example:

import javax.swing.JOptionPane;

public class TestClass {
    public static void main(String[] args) {
        String answer;
        answer = JOptionPane.showInputDialog(null, "What number to multiply by 3?");
        int num = Integer.parseInt(answer);
        num = num * 3;
        JOptionPane.showMessageDialog(null, "The answer is " + num);
    }
}

Note that showInputDialog returns a String, so you'll have to convert the data to whatever format you need. If you have something more involved, then JOptionPane may not be the way to go.

Irrecusable answered 31/10, 2009 at 9:31 Comment(1)
Thanks for the answer. I can see it could work, but I probably want it character by character, so more ideas would be welcome.Racer
A
0
public static void main(String[] args) {
        // Mostrar un mensaje de bienvenida y pedir al usuario su nombre
        System.out.print("Por favor, introduce tu nombre: ");
        Scanner scanner = new Scanner(System.in);
        String nombre = scanner.nextLine();// luego definir otras variables

Alvinia answered 18/9, 2023 at 6:24 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Petunia
N
-1

if you are asking for a visual input, NetBeans provides a very easy way to manage visual components, as easy as drag-and-drop

how to do it:

  • Create a JFrame by right-click on your package > New > JFrame form
  • you can see a "source" tab and a "design" tab on top of the frame.
  • drag and drop your visual components (like Text Field from the Swing Control on the right menu)
  • after placing your component(s) on the Frame, right-click > Events > (then choose the type of event you want to handle for each component)

it may look difficult and scary for first-timers, but once you start playing with it, few minutes and you will enjoy your experiments ;)

Naseberry answered 31/10, 2009 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.