0

In Java I want to print after press enter to do an input but in the same line, e.g: 'Username: // User input // Other print'.

Doesn't matter if I need to print in the loop or in the find method, if the username is not founded by the method it should print "User not found" after the input in the same line, like this: Username: // if not found -> User not found. I tried verifiying with if user is null but before the find method is late, it prints other line.

User user;

        do {

            System.out.println(" ");
            System.out.print("Username:");
            String username = input.nextLine();

            user = LogicCtl.findUserByUsername(username);

        }
        while (user == null);

   public User findUserByUsername(String username) {
    
    EntityManager em = getEntityManager();

    Query query = em.createQuery("SELECT u FROM User u WHERE u.username = :username");

    query.setParameter("username", username);

    User user;

    try { user = (User ) query.getSingleResult(); } catch (NoResultException e) {

        System.out.print(" User not found");
        return null;

    }

    return user;

    }

2
  • Are you allowed to create a GUI? If so, you might try Java Swing. Commented Apr 13 at 21:33
  • You might look into the possibility that your environment could use ANSI.SYS escape codes to control motion of the cursor. Commented Apr 13 at 21:39

1 Answer 1

0

There is but you don't want it. That's called 'uncooked' mode. It's all or nothing: In uncooked mode, you can move the cursor where-ever you want it to be, add colours, overwrite characters, get each character typed immediately on System.in (instead of the whole line at once when the user pressed enter) and so on.

You don't want it because:

  1. It's very very complicated, for example, pressing enter no longer moves the cursor back but it does, if you don't intercept it, 'scroll one down'. So you get this:
Username: Bukakiewicz
                     Age: 20

Which you can fix by injecting positioning, but, complicated.

  1. Java doesn't actually have baked in libraries to make this feasible; instead, you need to figure out what kind of terminal you are running on and send the right terminal code symbols which depends a bit on your OS and other factors. This is many weeks of work and requires having all sorts of computers available to you. It goes against the thing java tries to do: Write it once, it runs more or less the same way pretty much everywhere. You can fix that with an external dependency, though.

  2. Most IDEs have baked in little terminal-esque things to run your app in, and a lot of these don't support this at all, so that means you can no longer debug your app inside your IDE and this complicates matters further.

If you really want all this the best strategy by far is to use a library that does a lot of the work for you, such as Lanterna.

But know that, if you all you wanted was to 'avoid' the newline entered by the user, that really is shooting down a mosquito with a bazooka.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah is a silly detail, it really doesn't matter, but at least I save a line each time that not found a user. I couldn't do it with a thread that print this stopping the main thread before the thread that prints or something similar?
Nope. Uncooked mode is the only way and you don't want to do that.
And in other languages like Rust are possible this?
It's possible in java. But you don't want to. The same applies to every language. If you absolutely insist, use what I told you to use: Lanterna.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.