Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    try on all lines with marks
    StudentMarks.marks[0] = keyboard.nextDouble();

    see if the class name help, doh got home and its trick or treat night :(

  2. #22
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi ian,

    i realised it was halloween because I have been having a nightmare all day!!!

    no that don't work either, giving a cannot find symbol error.

    do you think it could be to do with the StudentMarks class?

    another poster give me an alternative but that didn't work either so i reverted back to the original but he seemed to think that the following should be the correct way

    class StudentMarks {

    public double[] marks;
    // Create the array
    public StudentMarks(int i){

    marks = new double[i];
    }

    }

  3. #23
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    sorry I am being a numpty, its been along week I can only get that to work with a collect of objects. The only other way I can see is to extent the class so inherts the variables.

    cheers

    Ian J

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default

    The code you posted to create an array of double looks like it should compile. Then what? The variable marks is public so any class that has a reference to the StudentMarks object will able to look at/modify the contents of the marks array.

  5. #25
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    public class StudentMarksMain extends StudentMarks {

    public void setMarks(int i){
    marks= new double[i];
    }

    public static void main(String[] args) {
    // TODO code application logic here
    }

    }






    public class StudentMarks {

    double[] marks;

    }

    :o

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default

    You've posted some code.
    What is your question about it now?
    Is your question solved now?

  7. #27
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Use what I posted - that works... Your code didn't work because you declared a var public inside a constructor.
    I die a little on the inside...
    Every time I get shot.

  8. #28
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile java compilers available from browser

    Quote Originally Posted by ianjedi View Post
    I don't have compliler on this computer, but I think it may work like that.
    There are several of these, just invest ten minutes search:
    JXXX Compiler Service
    This is a remote compiling service for those who would either like to develop applications and/or applets but aren't fortunate enough to have access to one of the platforms to which the JDK has been ported, or who just don't want to go through the hassle of installing the complete JDK. In addition to compiling you can view any compiled applets directly without having to download them to your site first; this should speed up debugging applets a bit.
    Quote Originally Posted by ianjedi View Post
    I don't have compliler on this computer, but I think it may work like that.
    Java Code:
    StudentMarks marks= new StudentMarks(7);
    as Supamagier 10-31-2008 10:05 AM, several beginner issues here which will be covered, my version:
    Java Code:
    import java.util.Scanner;// A Scanner breaks its input into tokens using a delimiter pattern.
    
    // Rewritten without test compile, try one of the online compilers.
    
    class StudentMarksMain 
    {
        private StudentMarks studentMarks;//
        StudentMarksMain(int length)
        {
            studentMarks = new StudentMarks(length);
        }
        // Have a main so as to be able to use the program.
        public static void main(String[] args) 
        {
            try
            {
                Scanner dataInput = new Scanner(new File("studentGradeNumbers.dat"));
                // This approach allows variable student grade count without hardcode.
                if(dataInput.hasNextDouble())
                {
                    try
                    {
                        int counter = 0;
                        Double countOfGrades = new Double(dataInput.nextDouble());//
                        StudentMarksMain markupGradesheet = new StudentMarksMain(countOfGrades.intValue());
                        // Here we go.
                        do
                        {
                            if(markupGradesheet.studentMarks.setStudentMarks(dataInput.NextInt(),++counter))
                            {
                                System.out.println(Integer.toString(counter));// 
                            }
                        }
                        while(dataInput.hasNextInt());
                        dataInput.close();
                        return;// declare return from main as work is completed.
                    }
                    // The next token does not match the Float regular expression, or is out of range 
                    catch(InputMismatchException ime)
                    {
                        System.out.println(ime.getMessage());// 
                        dataInput.close();
                        return;// declare return from main so to avoid continuation in failed state.
                    }
                    // The input is exhausted 
                    catch(NoSuchElementException nsee)
                    {
                        System.out.println(nsee.getMessage());// 
                        dataInput.close();
                        return;// declare return from main so to avoid continuation in failed state.
                    }
                    // Scanner is closed
                    catch(IllegalStateException ise)
                    {
                        System.out.println(ise.getMessage());// 
                        dataInput.close();
                        return;// declare return from main so to avoid continuation in failed state.
                    }
                }
                else
                {
                    System.out.println("Internal error, consult program directions.\n Try free online compiler service at http://www.innovation.ch/java/java_comp_instr.html");//;
                    dataInput.close();
                    return;// declare return from main so to avoid continuation in failed state.
                }
            }
            catch(Throwable t)
            {
                System.out.println(t.getMessage());//
                return;//
            }
        }
    }
    // Student Marks Data Storage (?)
    class StudentMarks 
    {
        double [] gradeMarks;
        public StudentMarks(int StudentMarkCount) {
             gradeMarks = new double [StudentMarkCount];
        }
        
        // Make accessor method for getting.
        public double getStudentMarks(int index)
        {
            try
            {
                return gradeMarks[index];
            }
            catch(ArrayIndexOutOfBoundsException aiobe1)
            {
                System.out.println(aiobe1.getMessage());//
                return 0.0000;// Placeholder value should be used here, a negative value may indicate internal program error.
            }
        }
        // Make accessor method for setting.
        public boolean setStudentMarks(int index, double score )
        {
            try
            {
                gradeMarks[index] = score;
                return true;
            }
            catch(ArrayIndexOutOfBoundsException aiobe2)
            {
                System.out.println(aiobe2.getMessage());//
                return false;
            }
        }
    }
    Caveat Emptor, check and test build before using, I am waking up on Friday so this is handy to practice on. Note I built in exception handling and error indication but the central issue is delcaration of a class in another class along with constructor and method use. I normally detest OO way of life but this is the place for it ... credit where credit is due.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Page 2 of 2 FirstFirst 12

Similar Threads

  1. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 10:59 PM
  2. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 06:09 AM
  3. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 03:27 PM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 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
  •