Console Input Output Working with the Console Reading User Input and Formatting Output The system console terminal standard input and output A special window used to communicate with the user ID: 1043965
Download Presentation The PPT/PDF document "Java Basics Tutorial Part 3:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
1. Java Basics TutorialPart 3: Console Input / Output
2. Working withthe ConsoleReading User Inputand Formatting Output
3. The system console / terminal / standard input and outputA special window, used to communicate with the userUses a text-based input / output (command line interface)Displays text data (text lines)Reads user input (text lines)What is the Console (Terminal)?
4. Everything we read from the console comes as a StringReading user input:Everything we print to the console is converted to a StringReading User Input and Printing StringsScanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("Hello world!");System.out.println("Hello" + 123);
5. Formatting text and data using placeholdersFormatting OutputString firstName = "John";int age = 19;System.out.printf("%s is %d years old", firstName, age); // John Doe is 19 years olddouble a = 5.123;double b = 6.456;System.out.printf("%.2f", a + b); // 11.58%s - placeholder for text%d - placeholder for integer%f - placeholder for floating-point number2 digits after the decimal point
6. Read a name from the console and prints a greeting:Reading User InputScanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("Hello " + name);The result from the execution would be:InputOutput
7. Reading NumbersReading Integers and Floating-Point Numbers
8. Reading an integer number from the console in Java:Example: calculating a square area by given side aReading Integersint a = scanner.nextInt();int area = a * a;System.out.println(area);int num = scanner.nextInt();
9. Reading a floating-point number:Example: convert inches to centimetersReading Floating-Point Numbersdouble inches = scanner.nextDouble();double centimeters = inches * 2.54;System.out.println(centimeters);double num = scanner.nextDouble();
10. Concatenating Text and NumbersString firstName = "John";String lastName = "Doe";int age = 34;String result = firstName + " " + lastName + " | " + age;System.out.println(result); // John Doe | 34Concatenationint a = 5;int b = 11;String result = "a + b = " + a + b;System.out.println(result); // a + b = 511
11. Coding ExercisesConsole-based Input and Output in Java
12. To learn coding, you need to write code!Watching videos gives you only knowledgeSolving the hand-on exercises, gives you experience and practical skillsLearn by DoingWrite and submit the coding exercises!
13. Write a program to convert days to minutes:Read a single integer (the days to be converted)Convert the days to minutes (1 day = 24 hours * 60 minutes)Print the minutesProblem: Days to Minutes2Minutes = 28805Minutes = 7200
14. Creating a New Project in IntelliJ IDEA
15. Solution: Days to Minutesimport java.util.Scanner;…Scanner sc = new Scanner(System.in);int days = sc.nextInt();int hours = days * 24;int minutes = hours * 60;System.out.println( "Minutes = " + minutes);
16. Submission in the Judge Systemhttps://judge.softuni.org/Contests/Practice/Index/3253
17. Write a program to convert centimeters to inches:Read a floating-point number: centimetersConvert the centimeters to inches: divide by 2.54Print the calculated result in the format below:Problem: Centimeters to Inches55.00 cm = 1.97 inches2.72.70 cm = 1.06 inches
18. Solution: Centimeters to Inchesimport java.util.Scanner;…Scanner sc = new Scanner(System.in);double centimeters = sc.nextDouble();double inches = centimeters / 2.54;System.out.printf( "%.2f cm = %.2f inches", centimeters, inches);
19. Write a program to calculate the speed by time and distance:Read 2 floating-point numbers: distance and timeCalculate the speed needed to travel the specified distance for the specified timePrint the calculated result formatted to 2nd digitProblem: Calculate Speed1527.50152.26.82
20. Solution: Calculate Speedimport java.util.Scanner;…Scanner sc = new Scanner(System.in);double distance = sc.nextDouble();double time = sc.nextDouble();double speed = distance / time;System.out.printf("%.2f", speed);
21. Write a program to calculate a triangle area:Read from input a side a and height for that side haCalculate the area of a triangle by its side and heightPrint the area, formatted to the 2nd digit after decimal pointProblem: Triangle Area51025.003.11015.50
22. Solution: Triangle Areadouble a = sc.nextDouble();double h = sc.nextDouble();double area = (a * h) / 2.0;System.out.printf("%.2f", area);
23. Write a program to calculate a circle area and perimeter:Read a floating-point number: the radius of a circleCalculate the area and the perimeter of a circlePrint the calculated values formatted to the 2nd digit after the decimal pointProblem: Circle Area and Perimeter7Area = 153.94Perimeter = 43.98
24. Solution: Circle Area and Perimeterdouble radius = sc.nextDouble();double area = radius * radius * Math.PI;double perimeter = 2 * Math.PI * radius;System.out.printf("Area = %.2f%n", area);System.out.printf("Perimeter = %.2f", perimeter);The Math constant π = 3.14159265358979
25. Learn By Doing!Do Your Exercises!Now
26. ………Next StepsJoin the SoftUni "Learn To Code" CommunityAccess the Free Coding LessonsGet Help from the MentorsMeet the Other Learnershttps://softuni.org