Search code examples
jcreator

Begginer struggle at format string


It is telling me that String cannot be converted to system... but I really don't know what mistake did i made... still very new on software development.. please help me...

import java.util.Scanner;

public class Serioustake1 
{
    public static void main(String[]args)
    {
        int age;
        String a = "Donald John Trump" ,b = "Height :",c = "Age :", d = "Name : ";
        float height;   

        Scanner input = new Scanner(System.in);

        System.out.print("Please enter the height of president:");
        height=input.nextFloat();

        System.out.print("Please enter the age of president:");
        age=input.nextInt();

        System.out.println("\n\n\t\t\t$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println("\t\t\t\tPresident Details");
        System.out.println("\t\t\t$$$$$$$$$$$$$$$$$$$$$$$$$$$");

        System formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);
        System.out.print(formatString);

    }
}

Solution

  • The return type of String.format() is String

    So change

       System formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);
    

    to

       String formatString = String.format("%-15s%-10s\n%-15s%-10.1f\n%-15s%-10d",
                                              d,a,b,height,c,age);