javafontsbold

How to print user input bold using Font class


Here is a snippet of my program asking users to enter their ships' coordinates where I want the user input to be printed Bold.

import java.util.Scanner;
    public class WorkArea{
    public static void main(String[] args){
    Scanner obj=new Scanner(System.in);
    for(int j=0; j<2; j++){
    System.out.println("\\nPLAYER "+(j+1)+", ENTER YOUR SHIPS' COORDINATES.");
        for(int i=0;i<5;i++){
        System.out.println("Enter ship "+(i+1)+" location:");
        String inp = obj.nextLine();
        String[] arrOfStr = inp.split(" ");
    int x = Integer.parseInt(arrOfStr[0]);
    int y = Integer.parseInt(arrOfStr[1]);
    }
    }
    }
    }

For that I did import Font class and created object to implement that class like the following,

import java.awt.Font;
Font f=new Font("Monospaced", Font.BOLD, 18);e

right after that, how can i set the scanner object i.e obj into BOLD using this awt class and I want the coordinates to be printed BOLD on the console using Font object f. Do I need to create a static method to make the user input bold like the following or can i directly set the object into Bold.

public int setFont(Font f, Scanner obj) {
}

I have tried importing Font class and created object for the font class like the following,

 import java.awt.Font;
    Font f=new Font("Monospaced", Font.BOLD, 18);

    System.out.println("Enter ship "+(i+1)+" location:");
    obj.setFont(f);
    String inp = obj.nextLine();
    String[] arrOfStr = inp.split(" ");
    int x = Integer.parseInt(arrOfStr\[0\]);
    int y = Integer.parseInt(arrOfStr\[1\]);
    }

    public Font getFont(Scanner obj){

    }

Expecting users input to be BOLD on the console and need clarification on how to define getFont() method or setFont() to make inputs BOLD.


Solution

  • WorkArea.java

    Keywords: Terminal , ANSI escape codes

    import java.util.Scanner;
    public class WorkArea{
        public static final String GREEN_BOLD = "\033[1;32m";  // GREEN
        public static final String RED_BOLD = "\033[1;31m";    // RED
        public static final String BLUE_UNDERLINED = "\033[4;34m";   // BLUE
        public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
        public static final String RESET = "\033[0m";  // Text Reset
        public static void main(String[] args){
            Scanner obj=new Scanner(System.in);
            for(int j=0; j<2; j++){
                System.out.println(YELLOW_BACKGROUND  + GREEN_BOLD+"PLAYER "+(j+1)+", ENTER YOUR SHIPS' COORDINATES."+RESET);
                for(int i=0;i<5;i++){
                    System.out.println(BLUE_UNDERLINED+"Enter ship "+(i+1)+" location:"+RESET);
                    String inp = obj.nextLine();
                    String[] arrOfStr = inp.split(" ");
                    int x = Integer.parseInt(arrOfStr[0]);
                    int y = Integer.parseInt(arrOfStr[1]);
                }
            }
        }
    }
    
    

    Set Background color and bold , at end , RESET.

    System.out.println(YELLOW_BACKGROUND  + GREEN_BOLD+"PLAYER "+(j+1)+", ENTER YOUR SHIPS' COORDINATES."+RESET);
    

    Set underlined , at end , RESET

    System.out.println(BLUE_UNDERLINED+"Enter ship "+(i+1)+" location:"+RESET);
    

    Linux

    enter image description here

    Will not work on older Windows:

    Works on "newer versions" of Windows

    Windows wt (Windows Terminal)

    enter image description here

    Windows powershell

    enter image description here

    Windows cmd

    enter image description here