开发者

Unwanted carriage return when getting input from terminal

I've just started learning Java and have come across a little quirk I'm not sure how to solve. It's the same as this question but the solution there doesn't work here. The problem there was thought to be the result of something to do with Netbeans, and compiling the code form the command line may solve the problem, but I am compiling and running from the command line.

Basically, when I run the following code (snipped for brevity)

    System.out.println("Number: ");
    line = in.readLine();

I get the following output:

Numb开发者_JAVA技巧er:
//Input cursor is found here

I'm running the code using the Gnome Terminal that ships with Ubuntu. Does anyone know what's causing this?


Use print instead of println if you don't want the trailing line break:

System.out.print("Number: ");
try {
    line = in.readLine();
}

The Javadocs for println already say so:

Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').


Use System.out.print() instead of System.out.println()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜