why does this code print out results in numbers not the actual names of the students?? from the following data from the txt file:
why does this code print out results in numbers not the actual names of the students?? from the following data from the txt file:
om
IT 500 Business Analytics:94.6;
IT 512 Project Management:88;
IT 513
-------------------------------------------
Tim
MBA 503 Foundations of Business Statistics:85;
IT 501 Fundamentals of Java Programming:70;
IT 500 Business Analytics:64;
IT 516 Social Media Analytics:77;
IT 515 Organizational Behavior:50;
-------------------------------------------
Smith
MBA 503 Foundations of Business Statistics:20;
IT 509 Data Management:89;
IT 512 Project Management:90;
-------------------------------------------
Davis
MBA 503 Foundations of Business Statistics:77;
IT 501 Fundamentals of Java Programming:89;
IT 510 Information Technology Management:88;
IT 517 Big Data Analytics:90;
-------------------------------------------
Judge
MBA 503 Foundations of Business Statistics:95;
IT 501 Fundamentals of Java Programming:88;
// Code
import java.io.*;
public class classgpa {
public static void main(String[] args) {
//Read the file
File file = new File("/Users/Desktop/1.txt");
//Declare a temporary variable
String temp="";
//Decalre variable to store the record
int r =0;
//Declare variable to store the n umber of lines
int line=0;
//Declare variable to store the total
double total = 0.0;
//Declare the variable to store the gpa
double gpa=0;
try{
//Declare a buffer reader
BufferedReader br = new BufferedReader(new FileReader(file));
//Read line and store in the variable str
String str=br.readLine();
//Check if the string is not null
while ((str!= null))
{
//Read a line
str = br.readLine();
//Check if the line contains -- to indicate the next record
if (str.contains("--"))
{
//Increase the number of records
r++;
//Calculate gpa
gpa = total / line ;
//Initialize line to 0
line=0;
//Display the gpa
System.out.println("Gpa of Student "+r+": "+ String.format("%.2f", gpa));
//Initialize total to 0
total=0;
}
else
{
//Decalre a loop till the end of string
for (int i=0; i < str.length(); i++)
{
//Check if the character is : to retreive total marks
if( str.charAt(i) == ':')
{
//Increament i
i++;
//Check if end of line is reached
while(str.charAt(i)!=';')
{
//Store the character in variable temp
temp=temp+(str.charAt(i));
//Increment i
i++;
}
//Increment the number of lines
line++;
//Calculate the total
total = total + Double.parseDouble(temp);
//Initialize temp to empty space
temp="";
}
}
}
}
}
//Declare catch
catch(IOException e){
e.printStackTrace();
}
//Catch statement for contains method
catch (Exception other) {
//Calculate the gpa for the last student
gpa = total / line ;
System.out.println("Gpa of Student "+ ++r + ": "+String.format("%.2f", gpa));
//System.out.println("File ended");
}
}
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images





