0

Hi... I really have a big problem in this program... I dont know how to properly read a user input from a text file.

I will really appreciate your help. thanks.

import java.util.*;
import java.io.*;

//--------------------------in this part is my problem----------------------//
public void openFile() {

File myFile = new File("students.txt");

try {
BufferedReader in = new BufferedReader(new FileReader(myFile));
String n ,i,y,c, name, id, year, course;

try {
n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

while(n!=null && i!=null && y!=null && c!=null) {

name=n;
id=i;
year=y;
course=c;

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

stud.add(new Student(name,id, year, course));

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

}

} finally {
in.close();
}

} catch (FileNotFoundException e1) {
System.out.println("File not found: " + myFile);

} catch (IOException e2 ) {
e2.printStackTrace();
}
}

Attachments
public class Student{
    private String name;
    private String id;
    private String year;
    private String course;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public Student(){

    }

    public Student(String name, String id, String year, String course){
        this.name = name;
        this.id = id;
        this.year = year;
        this.course = course;
    }

    public void viewDetails(){
    System.out.println("Student Name: " + name + "  " + "Student ID: " + id +
            "  " + "Year & Course: " + year + "-" + course);
    }


    public String toString() {
        return name + " " + id + " " + year + " " + course;
}

}


import java.util.*;
import java.io.*;


public class myStudent {


    int count;
    private Boolean search=false;
    List <Student> stud = new ArrayList <Student>();
    private Student[] StudentList;

    public myStudent() {
    
    }

    public void add(Student a) {
        stud.add(a);
    }

    public void view() {

       for(int i=0;i<stud.size();i++){
        System.out.println(stud.get(i));

    }
}
//-----------------------------------------------------------------------------//
public void openFile() {

File myFile = new File("students.txt");

try {
BufferedReader in = new BufferedReader(new FileReader(myFile));
String n ,i,y,c, name, id, year, course;

try {
n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

while(n!=null && i!=null && y!=null && c!=null) {

name=n;
id=i;
year=y;
course=c;

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

stud.add(new Student(name,id, year, course));

n=in.readLine();
i=in.readLine();
y=in.readLine();
c=in.readLine();

}

} finally {
in.close(); 
}

} catch (FileNotFoundException e1) {
System.out.println("File not found: " + myFile);

} catch (IOException e2 ) {
e2.printStackTrace();
}
}
//-----------------------------------------------------------------------------//

    public int searchStud(String name)  {
        for(int i=0;i<StudentList.length-1;i++)  {
                if(StudentList[i].getName().equals(name))
                     return i;
            }
        return -1;
    }

public Boolean search(String name){

         for(Student s : stud){

                 if(s.getName().equals(name)){
                 System.out.println("Name found!");
                 view();
                 search=true;

                     }
                 else if (stud.lastIndexOf(s)==stud.size()){
                        if(s.getName().equals(name)){
                     System.out.println("Record not found!");
                     search=false;
                    }
                        }
                 else if(stud.isEmpty()){
               System.out.println("The File is Empty."); }

             }  return search;}

    public Boolean getSearch() {
        return search;
    }

    public void setSearch(Boolean search) {
        this.search = search;
    }

//-----------------------------------------------------------------------------//
         public void editName(String sname, String name){
          for(Student s : stud){
             if(s.getName().contentEquals(sname)){
                 s.setName(name);
                 System.out.println("Name changed!");
                 s.viewDetails();
                     }
             }
    }

     public void editId(String sname, String id){
          for(Student s : stud){
             if(s.getName().contentEquals(sname)){
                 s.setId(id);
                 System.out.println("Student ID changed!");
                 view();
                     }
             }
    }

     public void editYear(String sname, String year){
          for(Student s : stud){
             if(s.getName().contentEquals(sname)){
                 s.setYear(year);
                 System.out.println("Year changed!");
                 view();
                     }
             }
    }
          public void editCourse(String sname, String course){

          for(Student s : stud){
             if(s.getName().contentEquals(sname)){
                 s.setCourse(course);
                 System.out.println("Course changed!");
                 view();
                     }
             }
    }
//-----------------------------------------------------------------------------//

 public void delete(String sname) {
         for(Student s : stud){
             if(s.getName().equals(sname)){
                stud.remove(s);
                 System.out.println("Student File deleted!");

             }else
                 System.out.println("Record not found!");
             break;

         }
    }
 //-----------------------------------------------------------------------------//
        public Student[] getStudentList() {
        return StudentList;
    }

    public void setStudentList(Student[] StudentList) {
        this.StudentList = StudentList;
    }

}

import java.io.*;

public class StudentFile {

    myStudent stud = new myStudent();
    File file = new File("student.txt");
    int ch;


public int displayMenu() throws IOException {
int choice=0;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("[1] Add ");
    System.out.println("[2] View");
    System.out.println("[3] Edit");
    System.out.println("[4] Search");
    System.out.println("[5] Delete");
    System.out.println("[6] Exit");
    System.out.println("Enter choice: ");
    choice=Integer.parseInt(br.readLine());

    return choice;

    }


    public void addMenu(myStudent stud) throws IOException {
        String  name, id, year, course;

        File myFile = new File("students.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Add Student details");
            System.out.println("Enter Student name: ");
            name=br.readLine();
            System.out.println("Enter Student ID: ");
            id=br.readLine();
            System.out.println("Enter year: ");
            year=br.readLine();
            System.out.println("Enter course: ");
            course=br.readLine();

            stud.add(new Student(name, id, year, course));

            PrintWriter out = new PrintWriter(new FileWriter(myFile,true));
            out.print(name);
            out.print(" | ");
            out.print(id);
            out.print(" | ");
            out.print(year);
            out.print(" - ");
            out.println(course);
            out.close();

    }

        public void delMenu()throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            stud.openFile();
            stud.view();

                System.out.println("\nEnter Student Name to delete: ");
                String sname=br.readLine();

                stud.delete(sname);

    }

    public void searchMenu(myStudent stud) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String name;
        int location;
        
       
        System.out.println("Name:  ");
        name=br.readLine();
        location = stud.searchStud(name);
        if(location ==-1)  {
            System.out.println("Record not found.");
            
        }  else {
            System.out.println("Record found.");
            System.out.println("Phone Number : " + stud.getStudentList()[location].getId());
        }
            
    }


        public void searchMenu2()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter Student name:");
                String sname=br.readLine();

                stud.search(sname);

    }

 public void editMenu()throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String name, id, year, course;
        System.out.println("Edit Student Details");
        System.out.println("------------");
        stud.view();

        System.out.println("Enter contact name to edit: ");
        String sname=br.readLine();
        stud.search(sname);
        if(stud.getSearch()==true){
            System.out.println("Which one you want to edit?\n[1]Name\n[2]ID  ");
            System.out.println("[3]Year\n[4]Course");
            System.out.println("Enter your choice: ");
            ch=Integer.parseInt(br.readLine());
            if(ch==1){
                System.out.println("Enter new name: ");
                name=br.readLine();
                stud.editName(sname, name);

            }else if(ch==2){
                System.out.println("Enter student Id: ");
                id=br.readLine();
                stud.editId(sname, id);
            }
            }else if(ch==3){
                System.out.println("Enter Year: ");
                year=br.readLine();
                stud.editYear(sname, year);

            }else if(ch==4){
                System.out.println("Enter Course: ");
                course=br.readLine();
                stud.editCourse(sname, course);
            }
            else{ System.out.println("Thank you");}
     }


    
    public void mainProg() throws IOException {
        myStudent stud = new myStudent();
        int choice;
        stud.openFile();

            do{
            choice=displayMenu();
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by NormR1
0

Can you explain what you are trying to do and what the problem is?

Recommendations: Don't be so lazy when typing in the names of variables. Give them names that mean something. Do Not use short names like: n, i, y, c.

name=n;
id=i;
year=y;
course=c;

Instead of reading into one variable and copying to another, read the data directly into where you want it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.