Results 1 to 5 of 5

Thread: Student Program

  1. #1
    ROBELLARD is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Student Program

    am having an issue with my program. I am reading in a .dat file and the program goes through each line to take the three pieces and put them in a Student object. However, the user Smart is not set up correcty (intentional) and it is not picking up on the extra information. Can anyone help me out. The code is below.

    import java.util.Scanner;

    public class Student implements Comparable{

    private String name;
    private int semesterHours;
    private double qualityPoints;
    private double gpa;
    private boolean aw;

    public Student(String nameIn, int semesterHoursIn, double qualityPointsIn){
    name = nameIn;
    semesterHours = semesterHoursIn;
    qualityPoints = qualityPointsIn;
    aw = false;
    gpa = calculateGPA();
    }

    public String getName(){
    return name;
    }

    public int getSemesterHours(){
    return semesterHours;
    }

    public double getQualityPoints(){
    return qualityPoints;
    }

    public boolean getAW(){
    return aw;
    }

    public double calculateGPA(){
    gpa = qualityPoints/semesterHours;

    if(semesterHours < 30){
    if(gpa < 1.5){
    aw = true;
    }
    }
    else if(semesterHours < 60){
    if(gpa < 1.75){
    aw = true;
    }
    }
    else{
    if(gpa <2.0){
    aw = true;
    }
    }
    return gpa;

    }



    public int compareTo(Object other){
    int result;

    if(this.getName().equals((String)other)){
    result=0;
    }
    else{
    result=1;
    }
    return result;
    }

    }

    import java.io.; import java.util.; public class Driver{ public static void main(String[] args) throws IOException{ Student[] aw = new Student[15]; int awIndex = 0; Scanner reader = null; try{ reader = new Scanner(new FileInputStream(args[0])); } catch(FileNotFoundException f){ System.out.println("No file by that name was found"); System.exit(1); }

    Student[] list = new Student[15];
    int index =0;
    Integer semHours;
    Double qualPoints;
    String nameIn;


    while(reader.hasNext()){
    //boolean proper;
    //reader.useDelimiter(" ");
    //String[] pieces = reader.nextLine().split(" ");
    //if(pieces.length > 3){
    // continue;
    //}
    //else{

    String name = null;
    Integer semesterHours = null;
    Double qualityPoints = null;
    try{
    name = reader.next();
    }
    catch(InputMismatchException i1){
    System.out.println("i1");
    }
    catch(NullPointerException num1){
    System.out.println("num1");
    }
    try{
    semesterHours = Integer.parseInt(reader.next());
    }
    catch(NumberFormatException num2){
    System.out.println("num2");
    }
    catch(NullPointerException nul2){
    System.out.println("nul2");
    }
    try{
    qualityPoints = Double.parseDouble(reader.next());
    }
    catch(NumberFormatException num3){
    System.out.println("num3");
    }
    catch(NullPointerException nul3){
    System.out.println("nul3");
    }
    catch(InputMismatchException in3){
    System.out.println("in3");
    }
    String[] pieces = reader.nextLine().split(" ");
    list[index] = new Student(name, semesterHours, qualityPoints);
    System.out.println(list[index].getName() + " " + list[index].getSemesterHours() + " " + list[index].getQualityPoints());
    index++;


    }


    /* for(int count = 0 ; count < index; count++){
    if(list[count].getAW()){
    aw[awIndex] = list[count];

    System.out.println(aw[awIndex].getName() + " is on aw and number " + awIndex + " in the aw array");
    awIndex++;
    }
    }

    */


    Scanner input = new Scanner(System.in);
    String in = ("");

    while( !(in.equals("xxx")) ){
    try{
    in = input.next();
    Student test = (Student)Searching.linearSearch(list, in);

    if( test != null){
    System.out.print("found and ");

    if(test.getAW()){
    System.out.println("on AW");
    }
    else{
    System.out.println("not on AW");
    }
    }
    }
    catch(NullPointerException n){
    if(!(in.equals("xxx"))){
    System.out.println("Not Found");
    }
    }


    }


    }

    }

    dat file

    Smith 27 83.7 Jones 21 28.35 Walker 96 182.4 Doe 60 150.0 Wood 100 400.0 Street 33 57.4 Taylor 83 190.0 Davis 110 198.0 Smart 75 2 92.5 Bird 84 168.0 Summers 52 83.2

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Student Program

    You need to provide better information. "It's not working correctly" provides zero information.

  3. #3
    ROBELLARD is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Student Program

    Sorry about that. What I am looking for in my program is for the driver to read in student.dat, which formats the information as:
    name int double
    name int double
    ect.

    There is one student towards the end of the list that has the format:
    name int int double

    The point of this excercise is to make the read in information into a student object, which takes those 3 pieces of information in the dat file. Also, however, I am supposed to test my programs ability to catch exceptions. I am supposed to catch a NumberFormat( which i am trying to accomplish with using parseInt on the int and double read in lines in Driver's main while loop), FileInputException (accomplished), and an IOException (also stuck on). What happens right now is that when I run my driver, it results in the last piece of information in the wrong student line being ignored, which would make his gpa and academic warning boolean possibly incorrect.

    essentially what I want is to ignore that user by not altering the student array, catch the exception it sends, and move on to the next line on the data file, essentially the next iteration of the while.

    I hope that clears it up! I can send someone my actual files if that helps.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Student Program

    Data stored in a flat file should be correctly formatted. It is almost impossible to handle data that is not as it can be stored in so many different ways. What if I gave you a file that stored data as:
    int String double
    int double String
    String int int int int int int int int double
    double int String
    double String int
    etc

    If you are expecting data to be String int double then that is the only data that should be in the file. In the real world anyway.

    If this is some PITA requirement for your assignment then we can make the assumption that the String will always be the first element (index 0), the int will always be the second (index 1) and the double will always be the last (index length - 1). That way you can ignore as many invalid ints in between.

  5. #5
    Rajeevkumar is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Jsp:add student
    By mamtha in forum New To Java
    Replies: 6
    Last Post: 01-16-2012, 07:26 AM
  2. student need help
    By adamrain in forum New To Java
    Replies: 9
    Last Post: 11-28-2009, 11:10 PM
  3. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 05:31 PM
  4. help student
    By jvasilj1 in forum New To Java
    Replies: 15
    Last Post: 02-02-2008, 08:23 AM
  5. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 01:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •