Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove a character at the end of a printed line?

So I have this line of code:

public static void main(String[] args) {
    
    System.out.print("Enter a year: ");
    int a = 1004;    //a = 1004
    System.out.print("Enter another year: ");
    int b = 2020;    //b = 2020
    int min = a < b ? a : b;
    int max = a < b ? b : a;
    System.out.print("Output: ");
    for (int i = min; i <= max; i++) {
    if (i%4==0 && i%100 !=0) {
        System.out.print(i + ", ");  //Output: 1004, 1008, 1012, 1016, 2020,
    }
}
    
}

So the code will print out: Output: 1004, 1008, 1012, 1016, 1020,

How do I remove the last comma and replace it with a dot ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public static void main(String[] args)
{

System.out.print("Enter a year: ");
int a = 1004;    //a = 1004
System.out.print("Enter another year: ");
int b = 2020;    //b = 2020
int min = a < b ? a : b;
int max = a < b ? b : a;
System.out.print("Output: ");
for (int i = min; i <= max; i++)
{
    if (i==b)
    {
        System.out.print(b + ".");
        break;
    }
    if (i%4==0 && i%100 !=0 ) 
    {
       System.out.print(i + ", ");  //Output: 1004, 1008, 1012, 1016, 2020.
     }

}

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading