Home > Net >  Message appears after writing an input in java
Message appears after writing an input in java

Time:12-21

I'm new to Java and i was learning inputs with Scanner but i'm getting something odd, the message of "enter a number" doesn't show, it only appears after I enter a value, here is the code and the screenshot. I'm using java 11 btw. `

public class HolaMundoMain {

    public static void main(String[] args) {
        
        Scanner entrada = new Scanner(System.in);
        int numero;
        System.out.print("Enter a number: ");
        numero = entrada.nextInt();
        
        System.out.println("The number is: " numero);
    }
}

` I tried writing the sout line before declaring Scanner but it does not work

I was expecting that first it would appear "Enter a number: " and then i could write it, but i have to write it first and then the message appears enter image description here, the image shows my run of the said code. Have you made any changes to java code? It's possible that you wrote the statement that takes input first then sout then you compiled it and ran it for the first time, but after changing it you forgot to compile it again using "javac completeFileName" in CMD. I have a strong feeling you just need to recompile it and then run the program using "java completeFileName". Also please note that, completeFileName is a name of path of file. if you don't compile after making changes to a file, what happens is compiler runs the previously compiled version of it, which is basically a fileName.class file(you can even see it in folder), but when you do compile again a new fileName.class is created and then used to run.

CodePudding user response:

The code it's working on my end. Try to compile it using javac HolaMundoMain.java and run with java -cp . HolaMundoMain. Image of the code running

  • Related