2

Code:

import java.util.Scanner;
public class Try{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String : ");
    String s = sc.nextLine();
    System.out.println("The Entered String is : " + s);
    System.out.println("The Length of Entered String is : " + s.length());
    sc.close();
  }
}

Output:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

How can i achieve this in Java?

[EDIT]

The one and only aim is to give the user a exact terminal like experience while using the program.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
  • I'm not sure this can be achieved from Java itself as the key strokes are first handled by your command line and mapped to those control characters. Java will then receive the input from the shell, not the other way round. – Thomas Mar 31 '21 at 11:15
  • @Thomas as far as i know its possible for c ans c++ but java i am not sure – Jaysmito Mukherjee Mar 31 '21 at 13:45
  • @Thomas i will look into it and if i am able to find a solution i will update it here – Jaysmito Mukherjee Mar 31 '21 at 14:13
  • @Thomas but the link you have given is for C# but the Java Console class is quite limited and doesn't offer much! – Jaysmito Mukherjee Mar 31 '21 at 14:23
  • Oh yes, you're right. My bad, didn't check this. I'll delete the comment. – Thomas Mar 31 '21 at 18:28
  • Per the answer to your (C/C++ question)[https://stackoverflow.com/questions/66886650/avoid-control-sequenceslike-c-from-c-or-c-standard-inputstream] asking exactly the same thing - there is a (java-readline)[https://github.com/aclemons/java-readline] library. – Anya Shenanigans Mar 31 '21 at 21:42
  • @Thomas I did find the answer and have posted it below (incase you want to check it out) – Jaysmito Mukherjee Apr 01 '21 at 09:27

1 Answers1

1

So I did a lot of searching and googling and as of now I don't think it is possible to do so directly from Java.

But, but we can achieve this and much more with C++ or C.

So, the best and most optimal solution will be to use a C++ library for this.

Now I don't know much of any such preexisting library(for Java) so I wrote my own C++ code and called it from Java using JNI.

And if you think its too complicated to do it every time for such a small issue i have created a very simple Java Library that does all that.

Here is the GitHub Page: https://github.com/Jaysmito101/SeriousConsole

It is very simple to use it.

My code:

import com.jaysmito.sconsole.SeriousConsole;

import java.nio.file.*;
public class Main{
    public static void main(String[] args) throws Exception{
        SeriousConsole.initConsole(Path.of("").toAbsolutePath().toString() + "/libseriousconsole.so");
        SeriousConsole.print("Enter a String : ");
        String s = SeriousConsole.readLine();
        System.out.println("The String entered is: " + s);
    }
}

To compile :

javac -cp SeriousConsole.jar Main.java

To run :

java -cp .:SeriousConsole.jar Main

You can download the jar and so file form the GitHub link.

Note : This library is meant to be a better version of java.io.Console

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
  • Nice! Is it better than these ones? [1](http://software.clapper.org/javaeditline/), [2](https://github.com/aclemons/java-readline), [3](https://github.com/jline/jline2) – rustyx Apr 01 '21 at 11:04
  • @rustyx i don't know how to compare it depends on the one who would use it but the main difference is(will be as my library is not yet completed and i have planned some more things) that the libraries you stated are mostly the gnu readline wrapper but what i intend to do with my library is to have a `a better version of java.io.Console` and by that i mean more ways to control the console like colored outputs, as well as i intend to add `ncurses` and allow a whole lot of ways to control the console. – Jaysmito Mukherjee Apr 01 '21 at 11:27