I have to write a program that takes a student ID and a number as input and then lowers all student notes by that that number except for one student based on the inputed ID. Here's what i mean: String = "Simon, 12345, 75\n Nick, 23456, 85\n Frank, 34567, 97\n
there's the students' names then their id and then their grade. i have to grab one ID as input and keep that student's grade intact but lower all the other grades by the inputed number.
System.out.print("Quel est le matricule de la note à conserver ? ");
String matricule = Keyboard.readString();
System.out.print("Combien voulez-vous enlever ? ");
int baisse = Keyboard.readInt();
String temporaireD = "";
int débutÉlève = 0;
int finÉlève = 0;
for (débutÉlève = 0; débutÉlève < notesDéchiffrées.length(); débutÉlève = finÉlève 1){
finÉlève = notesDéchiffrées.indexOf('\n', débutÉlève);
String noteÉlève = notesDéchiffrées.substring(notesDéchiffrées.lastIndexOf(", "), finÉlève);
if (notesDéchiffrées.indexOf(matricule) == -1){
int note = Integer.parseInt(noteÉlève);
note = note - baisse;
String noteString = String.valueOf(note);
String nouvelÉlève = notesDéchiffrées.substring(débutÉlève, finÉlève);
}
else{
String bonÉlève = notesDéchiffrées.substring(débutÉlève, finÉlève);
continue;
}
}
CodePudding user response:
public static void decreaseGrade(String studentId, int reduction) {
String studentRecord = "Simon, 12345, 75\n Nick, 23456, 85\n Frank, 34567, 97\n";
String[] csvArray = studentRecord.split("\n");
for(String student : csvArray) {
String[] studentAttributes = student.split(", ");
String name = studentAttributes[0].trim();
String id = studentAttributes[1].trim();
Integer grade = Integer.valueOf(studentAttributes[2].trim());
if (!studentId.equals(id)) {
grade -= reduction;
}
System.out.println("Name: " name ", ID: " id ", Grade: " grade);
}
}
CodePudding user response:
The code seen in Answer by kladderradatsch looks correct. Here is an alternative using objects, streams, and lambdas, for fun.
Set the inputs.
String targetId = "23456";
int reduction = 10;
String input =
"""
Simon, 12345, 75
Nick, 23456, 85
Frank, 34567, 97
""";
Define a record to hold each student's info.
record Student( String name , String id , int grade ) { }
Process the input data to get a list of student objects.
List < Student > students =
Arrays
.stream(
input.split( "\n" )
)
.map(
row -> {
String[] fields = row.split( ", " );
return new Student(
fields[ 0 ] , // name
fields[ 1 ] , // id
Integer.parseInt( fields[ 2 ] ) // grade
);
}
)
.toList();
Process that list of student objects, this time replacing each student object with a new student object having a grade reduced except for our one target student ID where the grade remains unmodified. A ?:
ternary statement is used here in lieu of an if
statement.
List < Student > studentsRevised =
students
.stream()
.map(
student ->
student.id.equals( targetId )
? student
: new Student( student.name , student.id , student.grade - reduction )
)
.toList();
Dump to console.
System.out.println( "input = " input );
System.out.println( "students = " students );
System.out.println( "studentsRevised = " studentsRevised );
When run. Notice that Nick keeps his grade while the other two are reduced.
input = Simon, 12345, 75
Nick, 23456, 85
Frank, 34567, 97
students = [Student[name=Simon, id=12345, grade=75], Student[name=Nick, id=23456, grade=85], Student[name=Frank, id=34567, grade=97]]
studentsRevised = [Student[name=Simon, id=12345, grade=65], Student[name=Nick, id=23456, grade=85], Student[name=Frank, id=34567, grade=87]]