Easy java question

Discussion in 'OT Technology' started by purebad, Sep 9, 2006.

  1. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    if I have a class such like:

    public class students{
    String name;
    String id;
    String email;
    }



    then I want to create one of those objects in main such as:

    students teststudent = new students();

    I get the wonderful "non-static variable this cannot be referenced from a static context" error, what is the problem?
     
  2. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    can you post your main file? I really don'[t see anything wrong. Have i forgotten that much java? I may dl eclipse right quick..
     
  3. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    Maybe you need a constructor in the class. Add a function to the class that says:
    students(){}

    Also, good java for good java coding practice, you shouldn't have a plural class names. Rename you class Student
     
  4. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
  5. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    You were declaring your students class within your ReadFile class.

    Try this:
    note: You must rename your file to students.java

     
  6. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    The problems are:
    A) your data members need to have a public/private declaration
    B) you are only allowed to have 1 public class per file
    C) move your student class to student.java file
    D) when you compile the original file, it will automatically compile student.java also

    Those are the errors I saw but I'm not sure if that will clear up your problems...however, IMO you need to implement all of those anyways.
     
  7. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    great stuff guys, I appreciate it greatly. I am sort of under the gun so to say in that I have to learn how to program in java over the weekend for something due tuesday. You have definetly cleared some things up that simple internet examples dont.
     
  8. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    so now I'm trying to create a vector, into which I will put each student, via:

    Vector myVector = new Vector();
    myVector.addElement(new students());

    Obviously I have not added the functions to allow passing of the student info through the instantiation of a new student, but I'm taking baby steps.

    javac gripes at me with the following:

    warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector
    myVector.addElement(new students());


    could someone explain what the problem is? It would appear to me it doesn't like the type of object I am putting in there...
     
  9. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    What is the purpose of the program? It looks like you're just trying to read a file and save the student info. You don't need to save the entire file into the variable "chunk".
     
  10. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    I am going to be storing students information (trying to in a vector) from a file, each line of the file is a student record. then I will also be storing course information(provides courses and student ids who have taken them) from another file. I need to then cross reference both vectors and display a list alphabetically of students who have taken "course X". I realize this is probably not the best way to do it however.
     
  11. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    You don't really need to use vectors for this one. Give me a few minutes and I'll come up with something to help ya
     
  12. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    Here is a simple application to read in all the students, store them, sort them and print their information again. You just need to fill in a few functions. You should be able to use this to finish your project.


     
  13. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    That method appears to throw a checked exception and you aren't handling that exception. You need to use a try/catch block or rethrow the exeption in the method that calls the addElement() method. Keep in mind, checked exceptions (exceptions that are a subclass of Exception but NOT a subclass of RunTime Exceptions) MUST be handled by your code or the compiler will error.

    In the API it says that method is identical to the add method of the list class. When I looked at that method, I see it throws 3 different exceptions.

    If you simply want an easy solution just do something like this

    try{
    myVector.addElement(new students());
    } catch (Exception e){
    //code to handle error (print message, rethrow exception, etc.)
    }

    Something like that should clear up your error.
     
  14. purebad

    purebad I don't need your approval, right?

    Joined:
    Aug 25, 2006
    Messages:
    4,376
    Likes Received:
    0
    Location:
    Smyrna, GA
    making progress, but after I cleaned up some stuff and normalized my naming conventions, it says this function doesnt have a return, I think it clearly does....

    public int compareTo(Object otherStudent)
    {
    //comparator for Array.sort()
    int result = (this.lastName).compareTo(((Student) otherStudent).lastName);
    if(result == 0)
    {
    result = (this.firstName).compareTo(((Student) otherStudent).firstName);
    }
    return result;
    }


    ?
     
    Last edited: Sep 10, 2006
  15. YodaHart

    YodaHart New Member

    Joined:
    Jul 7, 2006
    Messages:
    605
    Likes Received:
    0
    Looks good so far.

    I got a different error, but you can try this instead:


     

Share This Page