Java - i have the program working here, I just need help aligning the columns co
ID: 3738332 • Letter: J
Question
Java - i have the program working here, I just need help aligning the columns correctly because they do not center correctly. For example, all the names need to be in the far left (which is not happening as of this point)
import java.util.Scanner;
import java.text.DecimalFormat;
public class HealthCheck {
public static void main(String[]args) {
//variables
Scanner reader = new Scanner(System.in);
DecimalFormat f = new DecimalFormat("##.0");
String name;
int heartRate;
int respiratoryRate;
int height;
int weight;
int systolicPressure;
int diastolicPressure;
double BMI;
int score;
System.out.println("Name Heart Rate Resp Rate Height Weight BMI BP Score");
//input
while(reader.hasNext()){
name = reader.next();
heartRate = reader.nextInt();
respiratoryRate = reader.nextInt();
height = reader.nextInt();
weight = reader.nextInt();
systolicPressure = reader.nextInt();
diastolicPressure = reader.nextInt();
BMI = (((double)weight)* 0.453592d)/((((double)height)*0.0254) * (((double)height)*0.0254));
if(heartRate == 0 || respiratoryRate == 0 || height == 0 || weight == 0 || systolicPressure == 0 ||
diastolicPressure == 0) {
System.out.println("Invalid record");
}
else {
System.out.print(name+" ");
}
score = 1;
if(heartRate >= 60 && heartRate <= 100){
score++;
System.out.print(heartRate+" ");
}
else {
System.out.print("!!"+heartRate+" ");
}
if(respiratoryRate >= 12 && respiratoryRate <= 18) {
score++;
System.out.print(respiratoryRate+" ");
}
else {
System.out.print("!!"+respiratoryRate+" ");
}
System.out.print(height+" ");
System.out.print(weight+" ");
System.out.printf(f.format(BMI)+" ");
if((systolicPressure >= 90 && systolicPressure <= 120)&&(diastolicPressure >= 60 && diastolicPressure<=80)) {
score++;
System.out.print(systolicPressure+"/" +diastolicPressure+" ");
}
else {
System.out.print("!!"+systolicPressure+"/" +diastolicPressure+" ");
}
for(int i=0; i System.out.print("*");
}
System.out.println();
}
}
in1.dat:
Smith 70 15 72 168 110 70
Oracle 70 0 62 130 110 70
Switch 59 15 70 175 110 81
Trinity 100 11 68 140 110 70
Morpheus 60 19 72 175 110 70
Cypher 80 18 82 176 110 85
Dozer 80 15 82 177 89 70
Dujour 70 15 62 0 110 70
Tank 80 15 70 174 110 70
Neo 101 15 72 175 110 59
Jones 110 19 69 220 110 55
Apoc 80 15 62 130 121 70 ----------------------------
Explanation / Answer
Use a format specifier to print data with specific format of a specific width. Define width of each column before printing it. An integer and string example is given below:
String formatInt = "%5d"; // To print 5 digit integers
System.out.format(formatInt, VARIABLE_TO_BE_PRINTED);
String formatStr = "%-50s"; // To print 50 characters string
System.out.format(formatStr, VARIABLE_TO_BE_PRINTED);
In this code add below statements:
For printing name:
String formatStr = "%-20s"; // Assuming max length of a name is 20 chars
System.out.format(formatStr, name);
All the other columns are of width 3 so print as below:
String formatInt = "%3d";
System.out.format(formatInt, heartRate);
System.out.format(formatInt, respiratoryRate);
System.out.format(formatInt, height);
System.out.format(formatInt, weight);
System.out.format(formatInt, diastolicPressure );
System.out.format(formatInt, systolicPressure);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.