Read Inputs of Several Data Types

Solve this Problem
Easy10 min
Topics
Companies
Given a student's name (string), age (int), grade (a single character), isEnrolled (boolean) and marks (an int array), return five lines describing them: the name, the age, the grade, "yes" or "no" for enrolment, and the total of all marks. Real programs read many kinds of input in one go, and each kind is read and stored differently: primitive typesPrimitive TypesBasic built-in value types such as int, double, char and boolean, as opposed to strings and arrays. like int and boolean, single characters, whole strings, and collections like arrays. In Java you would call nextInt(), next().charAt(0), nextBoolean() and nextLine() on a Scanner; in C, scanf with %d, %c and %s; in Python everything comes from input() and is converted with int(), float() or a comparison. A decimal value would be read the same way with nextDouble() or %lf. Here the values arrive already typed as parameters, so the focus is on using each type correctly — taking a char out of a string, turning a boolean into words, and walking an array.

Test Case 1:

Input:name = "Asha", age = 19, grade = "A", isEnrolled = true, marks = [80, 75, 90]
Output:["Name: Asha", "Age: 19", "Grade: A", "Enrolled: yes", "Total marks: 245"]
Explanation:The boolean true is shown as "yes"; the three marks add up to 245.

Test Case 2:

Input:name = "Dev", age = 22, grade = "C", isEnrolled = false, marks = [40]
Output:["Name: Dev", "Age: 22", "Grade: C", "Enrolled: no", "Total marks: 40"]
Explanation:false becomes "no", and a single mark is its own total.

Constraints

  • ◆1 ≤ name.length ≤ 30
  • ◆1 ≤ age ≤ 120
  • ◆grade is exactly one character
  • ◆1 ≤ marks.length ≤ 10
  • ◆0 ≤ marks[i] ≤ 100

Try the Dry Run

Approach & Solutions

Explicit Loop over MarksGood

Handle each input according to its type. The name is already text. The age is an int. The grade arrives as a one-character string, so take its first character to get a char. The boolean is turned into "yes" or "no". The marks array is added up with a plain loop. Finally, turn every value into one line of text.

TimeO(m)
SpaceO(1) extra
class Solution { public String[] describeStudent(String name, int age, String grade, boolean isEnrolled, int[] marks) { char gradeChar = grade.charAt(0); int total = 0; for (int m : marks) { total += m; } String[] lines = new String[5]; lines[0] = "Name: " + name; lines[1] = "Age: " + age; lines[2] = "Grade: " + gradeChar; lines[3] = "Enrolled: " + (isEnrolled ? "yes" : "no"); lines[4] = "Total marks: " + total; return lines; } }
Built-in SumOptimal

Same type handling, but let a library function add the marks: Arrays.stream(marks).sum() in Java, sum() in Python, accumulate in C++ and reduce in JavaScript. The result lines are written out directly in one list. C has no built-in sum, so it keeps the loop.

TimeO(m)
SpaceO(1) extra
class Solution { public String[] describeStudent(String name, int age, String grade, boolean isEnrolled, int[] marks) { char gradeChar = grade.charAt(0); int total = Arrays.stream(marks).sum(); return new String[] { "Name: " + name, "Age: " + age, "Grade: " + gradeChar, "Enrolled: " + (isEnrolled ? "yes" : "no"), "Total marks: " + total }; } }

Related Problems