Results 1 to 18 of 18
  1. #1
    RBNSN is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Problems concerning Static methods and class.

    The class filereadingusingscanner.java is the entry point fo the application. It has a method to instantiate an object of type Student. It also requests for a File name. Later, sends this file name and Student object to a static method called toFile(Student stu, String filename), so that the Student deatils can be written into a file. But there appears to be strange errors while I try to access the method and when I try to type simple
    System.out.println("Enter preferred file name.");
    This line of code at Line 53 causes the IDE to give an error message, "cannot find symbol". But the System.out.println line in the above lines are okay.
    The code is below.
    Please do explain the error messages and the problem concerning the static method.
    Please do take a look at the attachments I sent.
    Thanking in you in advance,
    RBNSN
    CODE OF filereadingusingscanner.java

    package newpackage;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    import newpackage.Student;
    import newpackage.HasNextLineDemo;
    import java.util.Scanner;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import newpackage.HasNextLineDemo;

    /**
    *
    * @author c1272e
    */
    public class filereadingusingscanner {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    //A method to instatiate a Student type object
    /*Called Register*/
    public static Student register() {
    Student kid = new Student();
    Scanner scnr = new Scanner(System.in);

    System.out.println("Enter information about the student");
    System.out.print("Student ID: ");
    kid.StudentIdentificationNumber = scnr.nextInt();
    System.out.print("First Name: ");
    kid.FirstName = scnr.next();
    System.out.print("Last Name: ");
    kid.LastName = scnr.next();
    System.out.print("Number of credits so far: ");
    kid.CreditsSoFar = scnr.nextInt();
    System.out.print("Grade point average: ");
    kid.GPA = scnr.nextDouble();

    return kid;
    }
    //Create a Student object
    //and assing a variable to hold the address
    Student newStudent = register();
    System.out.println("Now we will persist this data into a file.");
    System.out.println("Enter preferred file name.");
    Scanner scnr = new Scanner(System.in);
    String filename = scnr.nextLine();
    HasNextLineDemo.toFile(newStudent, filename);
    int testStatHolder = HasNextLineDemo.testStat();
    }


    }

    CODE OF HasNextLineDemo.java
    /**
    *
    * @author c1272e
    */
    public class HasNextLineDemo {
    public static int testStat(){
    return 5*10;
    }
    public static void toFile(Student stu, String filename){

    try{
    File fileHandle = new File(filename);
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileHandle));
    out.writeObject(stu);
    out.close();
    //return "The arraylist has been saved to a file called" + filename;
    }
    catch(Exception e){
    //return "Error saving to File.";
    System.out.println("Error saving to File.");
    }

    }

    CODE OF Student.java
    package newpackage;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author c1272e
    */
    public class Student {

    public int StudentIdentificationNumber;
    public String FirstName;
    public String LastName;
    public int CreditsSoFar;
    public double GPA;

    public String toString(){

    return String.format("%s:%d\n%s:%s\n%s:%s\n%s:%d\n%s:%f\n ","StudentId",StudentIdentificationNumber,"Firstna me",FirstName,"Lastname",LastName,"CreditSoFar",Cr editsSoFar,"GPA",GPA);

    }

    }

    I am terribly sorry, I tried to put up the screenshot but due to the file size limitation put up by Java forumsI couldn't. Will some one tell me what is wrong with my
    Attached Thumbnails Attached Thumbnails Problems concerning Static methods and class.-first-part-main-programme.jpg   Problems concerning Static methods and class.-second-part-main-programme2ndpart.jpg  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What errors do you get when you compile the program using the javac command?
    I don't know what your IDE is trying to say.

    Can you post the errors you get when compiling the program?

    Please use code tags when posting code.

  3. #3
    Join Date
    Feb 2009
    Posts
    15
    Rep Power
    0

    Default

    You must first create the student: Student newStudent = new Student(); then you can declare it equal to something else. The error is looking for something like: Student newStudent = new Register(); but from my understanding of your code this is wrong, so you need to create the student with the first line of code I gave you.

  4. #4
    Join Date
    Feb 2009
    Posts
    15
    Rep Power
    0

    Default

    Your main problem is with your brackets. You need to end the 'public static void main(..) {' before trying to start 'public static Student register() {'
    public static void main(String[] args) {
    // TODO code application logic here

    //A method to instatiate a Student type object
    /*Called Register*/
    public static Student register() {

  5. #5
    RBNSN is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks everyone for your support, I moved the static method definition out fo the main() method as thehighlander12 said and the errors are gone. But when I try to run the programme, there seems to be some errors given out by IDE. Please take a look BELOW at the errors and share with me what they mean,


    run:
    Now we will create an object of type Student.
    Enter information about the student
    Student ID: 100
    First Name: WES
    Last Name: LEY
    Number of credits so far: 3,50
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050)
    at newpackage.filereadingusingscanner.register(filere adingusingscanner.java:41) at newpackage.filereadingusingscanner.main(filereadin gusingscanner.java:74)
    Java Result: 1BUILD SUCCESSFUL (total time: 24 seconds)

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You are trying to read an int for number of credits, but you have entered a double.

    Java Code:
    System.out.print("Number of credits so far: ");
    kid.CreditsSoFar = scnr.nextInt();
    are your offending lines.

    Berkeleybross

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    errors given out by IDE
    Those errors look like they are from the java command at execution time, not from an IDE.

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    @Norm: You're right, it is at runtime. He's confused the terms because he is using netbeans which has its own console inside the IDE.

    The problem is at runtime, he's reading an int ("scanner.nextInt()") and he then typed in something which is not an int ("3,50")

    berkeleybross

  9. #9
    RBNSN is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Post

    Thank you very much norm and berkleybross for your input. There seems to be some more problems during runtime. I believe from the messages the problem seems to be centered around line 79. When I am trying to pass the object of type Student to the function toFile().

    HasNextLineDemo.toFile(newStudent, filename);

    But I can't understand the message given out in the console for me to understand it fully.
    The following is the Output in the console of NetBeans:

    Enter information about the student
    Student ID: 10
    First Name: PAUL
    Last Name: OCTOPUS
    Number of credits so far: 235
    Grade point average: 3.60
    Now we will persist this data into a file.
    Enter preferred file name.
    Octopussy
    Exception in thread "main" java.io.NotSerializableException: newpackage.Student
    at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1156)
    at java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:326)
    at newpackage.HasNextLineDemo.toFile(HasNextLineDemo. java:33)
    at newpackage.filereadingusingscanner.main(filereadin gusingscanner.java:79)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 minute 17 seconds)


    Thanking you all in advance, again!:)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The stack trace you've posted shows that the code at line 79 called the toFile() method.
    In the toFile() method at line 33 there is a call to writeObject() .

    Have you read the API doc for the writeObject() method? It describes the problem and why you are getting the error.

    Why are you using the writeObject() method? That seems like an advanced method to be used by a beginning student.

  11. #11
    chimanet is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    you have to implement the Serializable interface in class Student for u to b able to write objects to file

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chimanet View Post
    you have to implement the Serializable interface in class Student for u to b able to write objects to file
    Thanks for pointing this out. Positive rep for you.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    read the API doc for the writeObject() method
    That was pointed out in the doc:
    NotSerializableException - Some object to be serialized does not implement the java.io.Serializable interface.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    That was pointed out in the doc:
    True. But I can't "rep" you as the forum software says that I need to spread the rep around.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Of course. My messages often ask the OP to read the API doc vs my copying and pasting the relevant sections of it. The more the OPs get used to finding their own answers in the API doc the better for them. Too many posters try to save them the effort and then they never learn.

  16. #16
    RBNSN is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Thank you all, I have managed to get programme to run and write the object to a file. But now dobts have emerged about the file I have created.
    The following is the content of the IDE console:

    run:
    Now we will create an object of type Student.
    Enter information about the student
    Student ID: 100
    First Name: PAUL
    Last Name: OCTOPUSS
    Number of credits so far: 10
    Grade point average: 3.99
    Now we will persist this data into a file.
    Enter preferred file name.
    OCTOPUSS.txt
    BUILD SUCCESSFUL (total time: 1 minute 2 seconds)


    Here are the contents of the file I created:
    sr newpackage.StudentF#hD I CreditsSoFarD GPAI StudentIdentificationNumberL FirstNamet Ljava/lang/String;L LastNameq ~ xp
    @Q dt PAULt OCTOPUSS

    Attached hererwith the screenshot of the file in text editor. Is there anythong wrong with the file it seems to be different from waht I intended.
    Attached Thumbnails Attached Thumbnails Problems concerning Static methods and class.-octopuss.jpg  

  17. #17
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    The file looks like it *should* be alright.
    The random characters and boxes are caused by codes which conincide with asciii characters - you can usually ignore them.
    To test if it is working properly, simply read the file using an object reader.

    Berkeleybross

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Are you trying to read a serialized object written to a file in a text editor? That doesn't make any sense. Try opening a .doc file in Notepad. The results will not be formatted like it would be if you opened the .doc file in Word. Same problem. The file has control char in it.
    To serialize an object, java adds control characters to the data so that the object can be reconstructed. When you write the serialized object to file, it has control characters in it. That's what you are seeing when you open the file in a text editor.

    As Berkeleyboss says, you need to read the file with an object reader to put it back together again.

Similar Threads

  1. do static methods cause synchronization problems
    By rajakumar.tu in forum Advanced Java
    Replies: 2
    Last Post: 12-24-2009, 08:39 AM
  2. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 01:58 PM
  3. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 06:10 AM
  4. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 01:20 AM
  5. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 01:24 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
  •