-1

My current issue is couldn't get my program to display the user's desired student info when entering names (both given name and surname) out on to console.

to read data from text file and store it:

BufferedReader file = new BufferedReader(new FileReader("student.txt"));
            
            String line = "";
            
            List<Student> student = new ArrayList<>(10);
            
    
            while((line = file.readLine()) != null)
            {
                
                part = line.split(", ");
                type = part[0];
                title = part[1];
                firstName = part[2];
                lastName = part[3];
                studentNumber = Long.parseLong(part[4]);
                birthDay = Integer.parseInt(part[5]);
                birthMonth = Integer.parseInt(part[6]);
                birthYear = Integer.parseInt(part[7]);
                

                
                if(type.equals("Research"))
                {
                    //Research Student
                    int proposal = Integer.parseInt(part[8]);
                    int oral = Integer.parseInt(part[9]);
                    int thesis = Integer.parseInt(part[10]);
                    student.add(new ResearchStudent(title, firstName, lastName, studentNumber, birthDay, birthMonth, birthYear, proposal, oral, thesis));
                }

my source code for prompting user inputs and displaying info:


if(select == 6)
                    {
                        switch(selectStudent)
                        {
                            case 1:
                                
                                System.out.println("Enter name of student with both surname and given name: ");
            
                                String name = keyboard.nextLine().trim();
                                String fullName = firstName +lastName;
//                              
                                BufferedReader case6 = new BufferedReader(new FileReader("student.txt"));
                                String nextline = " ";
                                while((nextline = case6.readLine()) != null)
                                {
                                        if(name.equalsIgnoreCase(fullName))
                                        {
                                            ((ResearchStudent) student).display();
                                        }
                                }
applePine
  • 19
  • 4
  • `while(file.readLine() != null);` **1.** there's a semicolon at the end, making this loop pointless. **2.** You read the line, but you never store it anywhere so the result is lost. – QBrute Jul 07 '20 at 08:17
  • @QBrute care to turn that into an answer? – Mark Jul 07 '20 at 08:20
  • @Mark no need, this topic has been covered hundreds of times already. Also, there are already two answers here – QBrute Jul 07 '20 at 08:22
  • Is there any recommended source I can read up in order to improve my IO skills? – applePine Jul 07 '20 at 08:32

2 Answers2

1

first, delete this line.

line = file.readLine();

then while statement should be

while((line = file.readLine()) != null){

it should work. when u try let me know. The problem in ur code is u assign

line=file.readLine()

just once

While(file.readLine()!=null) 

is not an assignment. And u should delete While semicolon.

egemenakturk
  • 365
  • 1
  • 17
1

You should remove the first call to line = file.readLine(); as it will read the first line outside of the loop, so the object won't be constructed. Also the loop should be while((line = file.readLine()) != null)

Ayyoub
  • 178
  • 2
  • 13
  • This is not an absolute solution. the line should be updated for each line of reading. After ur edit, it is the same as my answer don't duplicate pls. – egemenakturk Jul 07 '20 at 08:23