Skip to main content

Very Basic Question from a Newbie

1 reply [Last post]
Joined: 2007-07-26

hello, I hope I'm not too in over my head here with you guys asking such a basic question.

I'm using NetBeans IDE 5.5.1 and when I try to run my project, the output window asks me for the the input before displaying the message asking me for the input. For example, when I write this code:

import java.util.Scanner;

public class Main {

public static void main(String[] args)
Scanner input = new Scanner (;

System.out.print("Hello, what is your name? ");
String name = input.nextLine();

System.out.printf( "Hello %s", name);

I have to type my name in before it asks me what my name is. After I input my name, the rest of the output comes out like so: Hello, what is your name? Hello joey.

Why does it do this since from the command prompt it works like it should?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-12-18

Try flushing the standard output stream before reading from input. If you wish data to be displayed, the flush method should be called on its stream. Some streams flush automatically on a newline character, others flush on every character, most only flush when their buffer is full.