Unit 5: Discussion - Part 2 2626 unread replies.2727 replies. Question Read the lecture for Chapter 5 and then answer the following:. Present an ORIGINAL example of the correct use of the printf method. This example must show how integers, doubles and Strings are properly formatted for display. At least one integer, one double, and one String must be printed using printf. You are encouraged to consult the textbook and any other sources you deem relevant, but come up with an example of your own. Show a complete Java program and a screen capture with a sample output.
import java.util.Scanner;
public class PrintEx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i;
double d;
String s;
System.out.println("Enter String : ");
s=sc.next();
System.out.println("Enter integer : ");
i=sc.nextInt();
System.out.println("Enter double: ");
d=sc.nextDouble();
//printing the 4 spaces before int
System.out.printf("%4d\n",i);
//printing only 2 fraction values
System.out.printf("%.2f\n",d);
System.out.printf("%s",s);
}
}
Get Answers For Free
Most questions answered within 1 hours.