1

I am trying to make a simple code which continously checks the user input on if the number is positive or negative and that it would all be on 2 lines. First line being the user input and the second line being the output.

I am a beginner in coding and am not such a professional, but I have right now put the Scanner object in a while loop and it checks if the user input is positive. If it's negative then it would stop the program.

import java.util.Scanner;
public class basic {
    public static void main(String[] args){
        int numb;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Fill in a random number which ain't negative!");
        while((numb = scanner.nextInt()) > 0) {
            System.out.println("Again!");
        }
        {
            System.out.println("This is a negative!");
            System.exit(1);
        }
    }
}

What I want to do is that I get this as an output and only on 2 lines:

1 2 3 4 5 
Again!

And if I input a negative number on the line that it changes the 'Again!' to 'This is a negative!'

1 2 3 4 5 6 -8
This is a negative!

But with the code I have now I can only get this as an output and would get much more than just 2 lines:

1
Again!

2
Again!

-3
This is a negative!
Popeye
  • 11,839
  • 9
  • 58
  • 91
Hooiberg12
  • 57
  • 6
  • 1
    The basic console input/output in Java doesn't support this mode. There are non-standard libraries that give direct terminal addressing within Java, but that's definitely not beginner material. To do these things, it's better to use a GUI. – RealSkeptic May 01 '19 at 12:18
  • 1
    How do you constantly check the input for a change and then change the output in a string inside a GUI then so I get the same effect like I wanted? – Hooiberg12 May 01 '19 at 12:49
  • 1
    In a GUI, you have a frame (a window), and you have a field for the input, and when you enter a value in that field, you can change a label. You don't have to check constantly for anything. It's event-based. See [the Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/index.html) for example. – RealSkeptic May 01 '19 at 12:57

3 Answers3

0

This is console output so cannot be overwritten. But if you are really looking to the getting required o/p printed on the console, you can use list and keep pushing digits to it and print all the items of the list.

Poorvi Nigotiya
  • 438
  • 1
  • 4
  • 16
0

You could use \r to rewrite the last line. Consider that:

System.out.println("\r"+yourOutput+" > ")

It replaces the last line in the console with this one, that's how loading bars in linux are done by the way :D. Use this method to display your previous inputs/numbers. User input will be in the second line. When user has pressed enter and your program has gotten the user input as string, process it and do the same thing again, this time with yourOutput updated with new info:

System.out.println("\r"+yourOutput+" > ")
rainisr
  • 304
  • 3
  • 15
0

Let me welcome you to your first post on StackOverflow!

It is unusual to have java applications clearing the console output. That being said, it is possible, but not without calling OS commands. As mentioned in comments, this is a little beyond beginner material if you do this.

If you want to try it though, as seen here, you can clear the console by executing a native OS command. If you use the below code (copied from the link with a minor edit)

public static void clrscr(){
    //Clears Screen in java
    try {
        if (System.getProperty("os.name").contains("Windows"))
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        else
            Runtime.getRuntime().exec("clear");
    } catch (Exception e) {}
}

you can call this with clrscr() in your code and you should be able to clear the screen.

Now looking at your code, you won't just be able to slip this in as you will clear the output, thus clearing the past numbers. If you want it the way that you described where you have all the user's output displayed in succession, you'll need to re-print all their entries each time you clear the screen. Thus each time you get a user's input:

  1. You'll want to take that input and add it to a list of numbers
  2. Clear the screen
  3. Output that entire list
  4. Output your evaluation

Let me know if you have questions. :-)

Drake3
  • 36
  • 8