开发者

How to back up a line after a new line submitted by a user? Java

Suppose, you ask user to provide you some input via a console app in Java. They do and hit Enter. You get the string and do something in response. Say, you calculate some value based on the user's input and print it out.

How might I print out a response on the开发者_运维问答 same line as user's input? I'd like to (possibly) delete a new line character and print out a response next to his input.

Please advise how to do this using Java.

Thank you.


You cannot control the Console through basic Java.I think you can use JLine to control the Console.In java 6 u have java.io.Console class through which you can echo asterisk *'s when password has to be read. http://blogs.oracle.com/alanb/entry/java_io_console_is_finally


I have tried to implement this with the help of jcurses library and here is a demo of something you are looking for

import jcurses.system.CharColor;
import jcurses.system.InputChar;
import jcurses.system.Toolkit;

public class TestClass {

    public static void main(String[] args) {

        try {
            CharColor printColor = new CharColor(CharColor.BLACK, CharColor.WHITE);


            int i = 0;
            int j = 0;
            while (true) {
                StringBuilder str = new StringBuilder();
                InputChar c = null;

                do {
                    c = Toolkit.readCharacter();                  //Read each character
                    if (c.getCharacter() != 10) {                 //Do not print character if Return key
                        str.append(c);
                        Toolkit.printString(String.valueOf(c), i++, j, printColor);   //Print character as you type
                    }
                } while (c.getCharacter() != 10);
                Toolkit.printString(processInput(str.toString()), i, j++, printColor);
                i = 0;
                if (j == Toolkit.getScreenHeight()) {
                    Toolkit.clearScreen(printColor);
                    j = 0;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String processInput(String input) {
        return " Input processed";
    }
}


You can with ANSI-Codes. In Linux I never had a problem to use them, but on Windows, you have to install ANSI.SYS first.

import java.util.Random;

public class AnsiMove
{
        public AnsiMove ()
        {
                Random random = new Random ();
                System.out.print ("ESC[2J");

                for (int i = 0; i < 10000; ++i)
                {
                        int y = random.nextInt (23) + 1;
                        int x = random.nextInt (79) + 1;
                        char c = (char) (random.nextInt (95) + 32);

                        gotoXY (x, y);
                        System.out.print (c);
                        pause (1);
                }
        }

        void pause (int p)
        {
                try
                {
                        Thread.sleep (p);
                }
                catch (InterruptedException e)
                {
                        System.err.println (e);
                }
        }

        void gotoXY (int x, int y)
        {
                System.out.print ("ESC[" + y + ';' + x + 'H');
        }

        /** */
        public static void main (String args[])
        {
                new AnsiMove ();
        }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜