In the real environment we rarely work with the same values all the time. Prompt the user to provide you the integers, take input from console.
![TASK 2. String Formatting.
Review printf() method, Implement the following code, make sure it runs without errors.
3 public class ReportSum2 {
public static void main(String args[]) {
//assume we have 2 numbers a and b, a = 2, b = 5
int a = 2;
int b = 5;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
6
7
9
//assume we have 2 numbers a and b, a = 4, b = 4
a = 4;
b = 4;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
10
11
12
13
14
//assume we have 2 numbers a and b, a = 1000, b = 2000
a = 1000;
b = 2000;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
15
16
17
18
19
//assume we have 2 numbers a and b, a = 100, b = 101
a = 100;
b = 101;
System.out.printf( "Provided Integers: %4d and %4d, the total is %5d\n", a, b, (a + b));
}
20
21
22
23
24
25 }
In the real environment we rarely work with the same values all the time. Prompt the user to provide you the
integers, take input from console.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ec814c7-90f3-4435-9bb8-fcbab3f733ac%2F6192d204-0d8c-4794-8f51-0dd3c19adb40%2Fbp7yefg_processed.png&w=3840&q=75)

To take input from users we have to use the Scanner class.
Scanner sc= new Scanner(System.in);
int a=sc.nextInt();
int b= sc.nextInt();
from like this we can take input from users. After that we will print a,b as well as their sum. We will use here printf method which belongs in Java PrintStream class. We use it to write a String formatted to output Stream.
It uses the specified format string and arguments.
We can use print/println here, then we don't have to use %4d. We can use then
System.out.printf("Provided integers: " +a +"and "+b+ " the total is "+(a+b));
Note: In the below code I have used Main as the class name as I am doing my code in the online editor, you can name your class according to your code.
Step by step
Solved in 2 steps with 2 images









