0

Create a student class with data members: name, roll number, city, total marks, Include get and display function and 3 queries functions. Implement the class by storing records of at least 3 students in a link list and solve the following queries:

  1. Display details of students belonging to a particular city.
  2. Count the number of students whose name starts with a particular letter.
  3. Display students’ details scoring the highest marks.

General code works but functions create exceptions with iterator such as no such element found or index out of bounds.

Code So Far:

import java.util.*;

class Student{

String name, rollnumber, city;
double totalmarks;
Scanner sc = new Scanner(System.in);
LinkedList <Student> link = new LinkedList<Student>();
void get(){
    System.out.println("Enter the Name of Student");
    name = sc.next();
    System.out.println("Enter the Roll Number of Student");
    rollnumber = sc.next();
    System.out.println("Enter the City of Student");
    city = sc.next();
    System.out.println("Enter the Total Number of Student");
    totalmarks = sc.nextDouble();
}
void display(){
    System.out.println("Name: "+name+"\t"+"Roll Number: "+rollnumber+"\t"+"City: "+city+"\t"+"Total Marks: "+totalmarks);
}

} class Query extends Student{

Student s = new Student();
Student m = new Student();
String city_search, city_temp;
double marks;
int count = 0;
char str1, str2;
void city_query(){
    System.out.println("Enter the city");
    city_search = sc.next();
    Iterator <Student> it = link.iterator();
    while (it.hasNext()){
        s = it.next();
        if(s.city == city_search){
            s.display();
        }
    }
}
void first_alphabet(){
    System.out.println("Enter the letter");
    str1 = sc.next().charAt(0);
    Iterator <Student> it = link.iterator();
    while (it.hasNext()){
        s = it.next();
        str2 = s.name.charAt(0);
        if(str1 == str2){
            s.display();
        }
    }
}
void highest_marks(){
    Iterator <Student> it = link.iterator();
    s = it.next();
    m = s;
    marks = s.totalmarks;
    while (it.hasNext()){
        s = it.next();
        if (marks < s.totalmarks){
            marks = s.totalmarks;
            m = s;
        }
    }
    m.display();
}

} public class Main {

static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
    Student st[] = new Student[10];
    Student s = new Student();
    Student s2 = new Student();
    Query q = new Query();
    int i = 0,n;
    System.out.println("Enter the number of records to enter");
    n = sc.nextInt();
    for (int j = 0; j<n; j++){
        st[j] = new Student();
        st[j].get();
        s.link.add(st[j]);
    }
    Iterator <Student> it = s.link.iterator();
    while (it.hasNext()){
        s2 = it.next();
        s2.display();
    }
    q.city_query();
    q.first_alphabet();
    q.highest_marks();
}

}

  • `s.city == city_search` [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo Mar 17 '22 at 17:41
  • As Pshemo said, your string comparison are not correct - it should be s.city.equals(city_search), assuming s.city won't be null. As for index out of bounds, in main your Student array st has a size of 10, so if the user tries to enter more records than that you'll get an error. I also noticed that highest_marks does not check it.hasNext() before calling it.next() the first time. – Zeke Rogers Mar 17 '22 at 17:58
  • @ZekeRogers thank you for helping. I’ve just started learning java so have been facing some troubles. But really appreciate your guidance. – Tushar Jaryal Mar 18 '22 at 19:34
  • @Pshemo thank you for your insight, I’ve learned something new. – Tushar Jaryal Mar 18 '22 at 19:36

0 Answers0