Tanjiro Kamado
Tanjiro Kamado

Reputation: 11

System.out.print() not working on my codes or is it my compiler?

I'm using goesApache NetBeans for my compiler and this how my code

package com.mycompany.mavenproject4;
import java.util.*;

public class MyZodiac {
    public static void main(String[] args) {
        Scanner zodiac = new Scanner(System.in);     
                
        System.out.println("Chinese Zodiac Sign");
        System.out.print("Enter your Name: ");
        System.out.print("");
        String name = zodiac.next();
        zodiac.nextLine();
       
        System.out.print("Enter your year of birth: ");
        int year = zodiac.nextInt(); 
}}

Then when I run this code it becomes like this

    Chinese Zodiac Sign
    <user input name>
    <user input year>
    Enter your Name: Enter your year of birth:

What I wanted is

Chinese Zodiac Sign
Enter your name: <user input>
Enter your year of birth:<user input>

Upvotes: 0

Views: 261

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

System.out.println includes a newline, which causes an implicit flush with buffered output (like System.out). You can flush explicitly. Printing "" does nothing though. In short, change

System.out.print("Enter your Name: ");
System.out.print("");
String name = zodiac.next();
zodiac.nextLine();

System.out.print("Enter your year of birth: ");

to

System.out.print("Enter your Name: ");
System.out.flush();
String name = zodiac.next();
zodiac.nextLine();

System.out.print("Enter your year of birth: ");
System.out.flush();

Upvotes: 1

Related Questions