I've been at this for hours and I kinda frustrated. I would love a helping hand if someone would please have a look at my code and tell me why I can't use Student1 in StudentRecWithinput.Update();

It works without it in there but it seems to only use the last line of data that i am reading in.

MAIN

package Database;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    Scanner Keyboard = new Scanner(System.in);
    String choice1 = "";

    System.out
            .println("Would you like to update a students GPA? Please Input Yes or NO.");
    choice1 = Keyboard.next();

    if (choice1.charAt(0) == 'y' || choice1.charAt(0) == 'Y') {
        recupdate();
    } else {
        System.out.println("Alright, Goodbye!");
    }
}

public static void recupdate() throws FileNotFoundException {
    Scanner Keyboard = new Scanner(System.in);
    int choice2;
    System.out.println("Here are the records!\n");

    StudentRec Student1 = new StudentRec();
    StudentRec Student2 = new StudentRec();
    StudentRec Student3 = new StudentRec();

    System.out
            .println("Who's gpa will you be edditing? Please input (1, 2 or 3)");

    choice2 = Keyboard.nextInt();
    if (choice2 == 1) {

        StudentRecWithInput.update(Student1);

    }

    if (choice2 == 2) {
        StudentRecWithInput.update(Student2);
    }

    if (choice2 == 3) {
        StudentRecWithInput.update(Student3);

    }
}
}

STUDENTREC

package Database;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class StudentRec {
// student related//

private String lastname;
private String firstname;
private String major;
private int age;
private static double gpa;
private static int credit;

// /non student related////
File info = new File("studentinfo.txt");
Scanner scan = new Scanner(info);
static int count = 0;

public StudentRec() throws FileNotFoundException {

    {
        count++;

        if (count == 2) {
            scan.nextLine();

        }

        else if (count == 3) {
            scan.nextLine();
            scan.nextLine();
        }

        firstname = scan.next();
        lastname = scan.next();
        age = scan.nextInt();
        setGpa(scan.nextDouble());
        major = scan.next();
        setCredit(scan.nextInt());
        System.out.println(firstname + " " + lastname + " " + age + " "
                + getGpa() + " " + major + " " + getCredit() + "");

    }

}

public static int getCredit() {
    return credit;
}

public void setCredit(int credit) {
    this.credit = credit;
}

public static double getGpa() {
    return gpa;
}

public void setGpa(double gpa) {
    this.gpa = gpa;
}

}

STUDENTRECWITHINPUT

package Database;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class StudentRecWithInput extends StudentRec {
static Scanner keyboard = new Scanner (System.in);


public StudentRecWithInput() throws FileNotFoundException {
    super();



}

public static double update(int credit, double gpa)
{
    double pastpoints = getCredit() * getGpa();
    int newcredhours = keyboard.nextInt();
    double newpoint = keyboard.nextDouble();
    double semestergpa = newpoint / newcredhours;
    double cumulate = (pastpoints + newpoint) / (newcredhours+ getCredit());

    System.out.print(pastpoints);

    return cumulate;

}

}

studentinfo.txt

Bob Bobbers 19 3.5 CPS 55 
John Johners 20 3.7 BIO 70
Kat Katters 21 3.8 ITC 100
有帮助吗?

解决方案

The signature of StudentRecWithInput

update(int credit, double gpa) 

expect int and double, but you are calling it with

StudentRecWithInput.update(Student1);

you need to change the signature to:

update(StudentRec student) 

to be able to pass student information into the method.

You also should read more about the difference between static and non static methods, as right now your code have no changes of compiling.

其他提示

You probably want to adjust your strategy. Consider using the following steps:

  1. Read your input file into a Vector of StudentRecord. (Your code tries to ready your input file each time you create a StudentRec)
  2. Then ask what record should be updated
  3. Then ask for update data for that record

So your main might have Vector<StudentRec>

While your StudentRec class would have an update method (no arguments) that you call when you need to change that StudentRec.

Exact details left as an exercise for questioner, since this appears to be a school task.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top