[JAVA - BEGINEER] Need to press ENTER twice to get the program to output everything

Hello guys, I've tried to search up what it could be, I found out that it could be related to the buffer. However I tried to use sc.nextLine(); to clear the buffer but it didn't seem to help.
import java.util.Scanner;

public class Ex2 {
public static void main(String[] args) {
/*
Write a program to display, in the form of bar graph, the number of students in a class that scored "Approved" and "Failed" on a set of subjects.
The program should start by asking the number of students in the class and the number of subjects and, for each subject will ask the name of the subject and the number of students with approval.

The output should look like this:
Subject: Math
- Approved: *******
- Failed: ***
*/

Scanner sc = new Scanner(System.in);

int numberOfStudents = sc.nextInt();
sc.nextLine(); //buffer clean
int quantityOfClasses = sc.nextInt();
sc.nextLine(); //buffer clean

int approvedStudents;
String subject;



while(quantityOfClasses > 0){
subject = sc.next();
approvedStudents = sc.nextInt();
sc.nextLine(); //buffer clean

studentsOverallStatus(numberOfStudents, approvedStudents, subject);

quantityOfClasses--;
}

}

public static void studentsOverallStatus(int numberOfStudents, int approvedStudents, String subject){

int failedStudents = numberOfStudents - approvedStudents;

System.out.printf("%nSubject: %s%n", subject);


System.out.print("- Approved: ");
while (approvedStudents > 0) {
System.out.print("*");

approvedStudents--;
}

System.out.printf("%n- Failed: ");
while (failedStudents > 0) {
System.out.print("*");

failedStudents--;
}
}
}
import java.util.Scanner;

public class Ex2 {
public static void main(String[] args) {
/*
Write a program to display, in the form of bar graph, the number of students in a class that scored "Approved" and "Failed" on a set of subjects.
The program should start by asking the number of students in the class and the number of subjects and, for each subject will ask the name of the subject and the number of students with approval.

The output should look like this:
Subject: Math
- Approved: *******
- Failed: ***
*/

Scanner sc = new Scanner(System.in);

int numberOfStudents = sc.nextInt();
sc.nextLine(); //buffer clean
int quantityOfClasses = sc.nextInt();
sc.nextLine(); //buffer clean

int approvedStudents;
String subject;



while(quantityOfClasses > 0){
subject = sc.next();
approvedStudents = sc.nextInt();
sc.nextLine(); //buffer clean

studentsOverallStatus(numberOfStudents, approvedStudents, subject);

quantityOfClasses--;
}

}

public static void studentsOverallStatus(int numberOfStudents, int approvedStudents, String subject){

int failedStudents = numberOfStudents - approvedStudents;

System.out.printf("%nSubject: %s%n", subject);


System.out.print("- Approved: ");
while (approvedStudents > 0) {
System.out.print("*");

approvedStudents--;
}

System.out.printf("%n- Failed: ");
while (failedStudents > 0) {
System.out.print("*");

failedStudents--;
}
}
}
This is for a university project so the Output must be the expected. Thanks in advance
5 Replies
JavaBot
JavaBot•5w ago
⌛ This post has been reserved for your question.
Hey @bk (🎃)! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
bk (🎃)
bk (🎃)OP•5w ago
Restarting the IDE solved the issue.
JavaBot
JavaBot•5w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
bk (🎃)
bk (🎃)OP•5w ago
/clone
JavaBot
JavaBot•5w ago
Post Closed
This post has been closed by <@265579618457616394>.
Want results from more Discord servers?
Add your server