1. Design a Java application that reads a sequence of student id, last name and
ID: 3531816 • Letter: 1
Question
1. Design a Java application that reads a sequence of student id, last name and first name from the user and then saves the user-entered data into an array of Student. The data input process will continue until the user enters -99 (negative 99) as the student id. The main( ) method will thentest the Student class. The student data will be saved into an array of Student. Add a method called displayStudents( ) that, when called, will display all students currently stored in that array. The final result should be....
Id<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
First name
Last name
1234
First
Last
1234
First
Last
1234
First
Last
2. Instead of manually entering the student data, revise your program such that it reads the data from a text file. (I will be able to save the text file myself, the data that should be in the text file should look like.....
1234 First Last
1234First Last
1234 First Last
1234 First Last
Id<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
First name
Last name
1234
First
Last
1234
First
Last
1234
First
Last
Explanation / Answer
// if both the program runs then rate.
// answer1:-- 'enter student details maually'
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Student
{
private int id;
private String firstName;
private String lastName;
public void setId(int id)
{
this.id=id;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public void setLastnmae(String lastName)
{
this.lastName=lastName;
}
public int getId()
{
return id ;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName ;
}
public static void main(String[] args)
{
Student s[]=new Student[100];
Scanner input1=new Scanner(System.in);
Scanner input2=new Scanner(System.in);
Scanner input3=new Scanner(System.in);
int i=0;
int option;
String firstName;
int id;
String lastName;
try
{
while(true)
{
System.out.print("enter student id or enter -99 to stop entering details: ");
id=input1.nextInt();
if(id==-99)
break;
System.out.print("enter firstName:");
firstName=input2.nextLine();
System.out.print("enter lastName:");
lastName=input3.nextLine();
s[i]=new Student();
s[i].setId(id);
s[i].setFirstName(firstName);
s[i].setLastnmae(lastName);
i++;
}
}catch(Exception ex)
{
System.out.println("something goe wrong while entering valeus" +
"program exiting.......");
}
displayStudents(s,i);
}
public static void displayStudents(Student s[],int i)
{System.out.println("student record..............");
for(int j=0;j<i;j++)
{
System.out.print(" student id: "+s[j].getId());
System.out.print(" first name: "+s[j].getFirstName());
System.out.print(" last name: "+s[j].getLastName());
System.out.println(" ");
}
}
}
// answer 2:-- reading student details from file
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Student
{
private int id;
private String firstName;
private String lastName;
public void setId(int id)
{
this.id=id;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public void setLastnmae(String lastName)
{
this.lastName=lastName;
}
public int getId()
{
return id ;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName ;
}
public static void main(String[] args)
{
Student s[]=new Student[100];
Scanner input1=new Scanner(System.in);
Scanner input2=new Scanner(System.in);
Scanner input3=new Scanner(System.in);
int i=0;
int option;
String firstName;
int id;
String lastName;
System.out.println("enter input file name :( example record.txt");
String fileName=input2.nextLine();
try
{
String line;
String data[];
FileReader file=new FileReader(fileName);
BufferedReader f = new BufferedReader(file);
while((line=f.readLine())!=null)
{
data=line.split(" ");
s[i]=new Student();
s[i].setId(Integer.parseInt(data[0]));
s[i].setFirstName(data[1]);
s[i].setLastnmae(data[2]);
i++;
}
}catch(FileNotFoundException ex)
{
}catch(IOException ex)
{
}
displayStudents(s,i);
}
public static void displayStudents(Student s[],int i)
{System.out.println("student record..............");
for(int j=0;j<i;j++)
{
System.out.print(" student id: "+s[j].getId());
System.out.print(" first name: "+s[j].getFirstName());
System.out.print(" last name: "+s[j].getLastName());
System.out.println(" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.