Home > database >  System.out.print doesn't work, println does
System.out.print doesn't work, println does

Time:12-28

I have problem with System.out.print method. It hit me for the first time. When I try to use System.out.print("File?") and then to use scanner.nextLine() my program doesn't print anything. Instead of this he waits for scanner.nextLine() to end and then he print "File?" string. When I use System.out.println("File?") method it works fine. What is the case? I use NetBeans.

My code: `

 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("File? ");
        String file = scanner.nextLine();
}

`

I tried to change method to println("File?") and it works, but I need to know why print("Files?") isn't work. I also tried to use System.out.flush() but it also didn't work.

CodePudding user response:

That seems rather strange. What if you try using scanner.next() instead of scanner.nextLine(), that would work if your input doesn't have spaces.

I tried your code and it works just fine. Are you typing in Output - Run tab in NetBeans?

CodePudding user response:

I tested your code in Netbeans 15 and it works as should be expected. You can see the prompt and what I entered in the screenshot.

enter image description here

  • Related