I have the following but my error checking is not working and my writing to a fi
ID: 3689203 • Letter: I
Question
I have the following but my error checking is not working and my writing to a file is not working it has created the file but is not writing to it?
Please make the Student List:
Enter First Name: hi
Enter your Last Name: by
Enter your Average: 454
Enter First Name: hello
Enter your Last Name: goodbye
Enter your Average: -454
#################################################################
def Main():
print('Please make the Student List: ')
Input()
#################################################################
def Input():
outfile = open('grades.txt', 'w')
#STUDENT INPUT +++++++++++++++++++++++++
Names = ["" for x in range(11)]
LName = ["" for x in range(11)]
Grade = [0 for x in range(11)]
for i in range(0,len(Names) - 1):
Names[i] = input('Enter First Name: ')
LName[i] = input('Enter your Last Name: ')
Grade[i] = input('Enter your Average: ')
try:
if Grade[i] > 100:
print ("Oops! That was not valid answer.")
Input()
elif Grade[i] <= 0:
print ("Oops! That was not valid answer.")
Input()
except:
print('Something wrong.')
else:
outfile.write(str(Names[i]) + ',' + str(LName[i])+ ': ' + str(Grade[i]) + ' ')
outfile.close()
Open()
####################################################################
def Open():
try:
infile = open("grades.txt", "r")
except:
print("Error: can't find file or read data")
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
line4 = infile.readline()
line5 = infile.readline()
line6 = infile.readline()
line7 = infile.readline()
line8 = infile.readline()
line9 = infile.readline()
line10 = infile.readline()
infile.close()
print()
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)
print(line6)
print(line7)
print(line8)
print(line9)
print(line10)
Main()
Explanation / Answer
import java.io.*;
import java.util.Scanner;
class studentdata {
public String name[][] = new String[10][10];
void getDetails(int n)
{
Scanner get = new Scanner(System.in);
int limit=n;
System.out.println("Enter "+limit+" Student Details ");
for(int i=0;i<limit;i++)
{
System.out.println("Enter Student #"+(i+1)+" Fname, Lname & AVG:");
for(int j=0;j<3;j++)
{
name[i][j] = get.nextLine();
}
}
display(limit);
}
void display(int limit)
{
System.out.println("Student Fname"+" "+"Lname"+" "+"AVG");
for(int i=0;i<limit;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(name[i][j]+" ");
}
System.out.println();
}
}
}
class studentdatabase {
public static void main(String args[]) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Students:");
int n = in.nextInt();
studentdata std = new studentdata();
std.getdata(n);
PrintStream output = new PrintStream(new File("Studentdata.txt"));
output.println("Student Fname"+" "+"Lname"+" "+"AVG");
output.println("======================================");
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
{
output.print(std.name[i][j]+" ");
}
output.println();
output.println("======================================");
}
output.close();
}
}
note-by using the above code with required modifications the given question can be answered.