public class Student {
private String courses = null;
}
.....Some code here.......
//Enroll in courses
public void enroll() {
//Get inside a loop and user hits Q
do {
System.out.print("Enter Course to Enroll(Q to quit): ");
Scanner in = new Scanner(System.in);
String course = in.nextLine();
if (!course.equals("Q")) {
courses = courses + "
" + course;
tuitionBalance = tuitionBalance + costOfCourse;
}
else{ break; }
}while ( 1!= 0);
}
......Some code here......
//Show Status
public String showInfo() {
return "Name: " + firstName + " " + lastName +
"
Grade level: " + gradeYear +
"
Student ID: " + studentId +
"
Courses Enrolled:" + courses +
"
Balance: $" + tuitionBalance;
}
}
Everything seems to be running just fine. But there's an extra null printed after the Courses Enrolled that sticks out like a sore thumb. How could I get rid of it? I've tried setting the String variable courses to null and also without assigning but doesn't seem to affect the result
Name: Frank Kuo
Grade level: 1
Student ID: 11001
Courses Enrolled:null
Math101
Balance: $0
question from:
https://stackoverflow.com/questions/65855279/program-printing-extra-null-that-i-cant-seem-to-get-rid-of 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…