bk (🎃)
JCHJava Community | Help. Code. Learn.
•Created by bk (🎃) on 10/24/2024 in #java-help
[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
This is for a university project so the Output must be the expected.
Thanks in advance
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--;
}
}
}
7 replies