Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: [Help] Problem with Class arrays, adding/editing and deleting

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Help] Problem with Class arrays, adding/editing and deleting

    Hello javaprogrammingforums,
    I'm new here and new to the language.
    I am trying to create a program as a side project to my studies but am having some difficulties.

    I would like to create a simple record system that holds information like:

    * Name
    * Date of Birth
    * Address
    * etc

    In this 'system' I would like a user be able to add new 'records' and edit or delete them once created.
    I know some basics of the language and I've been playing with code for a few days now, i know WHAT i want to do and sort of the WAY in which it is done
    but... i dont know HOW to go about writing it.

    I currently have 2 .java files

    here is the first:
    class Student {
      private String name;
      private String DoB;
      private String term;
      private String home;
      private String course;
      private int fees;
      private String rela;
      private int marks;
      private String prog;
     
    public Student(String n, String d, String t, String h, String c, int f, String cr, int m, String p) {
    name = n;
    DoB = d;
    term = t;
    home = h;
    course = c;
    fees = f;
    rela = cr;
    marks = m;
    prog = p;
     
      }
     
      public String print() {
        return name + "\n" + DoB + "\n" + term + "\n" + home + "\n" + course + "\n" + fees + "\n" + rela + "\n" + marks + "\n" + prog;
      }
     
     public String toString() {
     return "These are the Current Stored Details:\n" + print();
    }
     
    }

    Which i think is still working fine, however my second file has become more tempermental during the week as i've been adding code, mainly because i am not certain on what i'm doing :/

    public class StudentTest 
    {
      public static void main(String[] args) 
    {
    String n;
    String d; 
    String t; 
    String h;
    String c;
    int f; 
    String cr;
    int m; 
    String p;
     
        Student[] kid;
        kid = new Student[1];
        int i = 0;
     
       n = "Grant Searle";
       d = "09/12/1991";
       t = "Same as Home Address";
       h = "5 James Road, Gosport, Hampshire, PO13 0TF";
       c = "Computer Network Management & Design";
       f = 3275;
       cr = "Mother";
       m = 56;
       p = "Yes";
     
       Student startingKid = new Student( n, d, t, h, c, f, cr, m, p ) ;
       kid[i] = startingKid;
     
     
    for (i = 0; i < kid.length; i++)
    System.out.println(kid[i]);
     
     
    do {
    KeyboardInput in = new KeyboardInput() ;
     
       System.out.println("What do you want to do?");
       System.out.println("1) Add a Student Record");
       System.out.println("2) Edit a Student Record");
       System.out.println("3) Quit");
     
       String answer = in.readString();
     
       if(answer.equals("1") ){
     
    System.out.println("Please Enter Student's Full Name:");
    String inName = in.readString() ;
     
    System.out.println("Please Enter Student's Date of Birth:");
    String inDate = in.readString() ;
     
    System.out.println("Please Enter Student's Term Address:");
    String inTerm = in.readString() ;
     
    System.out.println("Please Enter Student's Home Address:");
    String inHome = in.readString() ;
     
    System.out.println("Please Enter Student's Course Name:");
    String inCourse = in.readString() ;
     
    System.out.println("Please Enter Student's Course Fees, £:");
    double inFees = in.readDouble() ;
     
    System.out.println("Please Enter Student's Closest Relative:");
    String inRela = in.readString() ;
     
    System.out.println("Please Enter Student's Marks:");
    int inMarks = in.readInteger() ;
     
    System.out.println("Please Enter Student's Progression: (yes/no)");
    String inProg = in.readString() ;
     
       }
       else if(answer.equals("2") )
    {
          System.out.println("What is the Student's ID?");
         //here i'd like some kind of index system so the user can type student 1 and that would edit array [0] by overwriting the 'stored' information
         // int studentNumber = Integer.parseInt(in.readString());
         // for(int y=0; y < newEntry; y++)
         // {
         //   if(studentNum[y] == studentNumber)
              //found the student
              //do the adding of new details, submit and overwrite
         //}
       }
    }
       while(!answer.equals("3"));
     
    // later add a delete option, ie. move current array into temp smaller array to shift elements instead of making null
     
    }
     
    }
    .

    In this second file, i have 1 person stored to begin with so the user can edit or delete right from the start.
    However i am having trouble with the rest, i know there is a lot to be added and changed as
    I've made mistakes and my syntax is probably horrid but if anyone could help me out i'd really appreciate it.

    Thank you


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    Welcome to Java Programming Forums.
    What do you need? Where do you need help? Is your code compiling? If not what are the errors or exceptions? Paste the error/exception messages when you try to compile/run your program.
    To get better help read the Forum Rules.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    Hey, earlier in the week i simply had kid [0] and the containing details which I could print that then ask for a user input which it did using KeyboardInput in = new KeyboardInput() ;
    but the program didnt do anything with that data as i hadn't written code for that yet.
    However now that i've attempted that bit of code i get errors.
    In my do while function, if a person wants to add a record i'd like what they enter to be stored in kid[1] as 0 is already filled, then later if they decided to add again the details would write into kid[2].
    here is the error i get at the moment:
    KeyboardInput in = new KeyboardInput() ;
    cannot find symbol
    symbol : variable answer
    location: class StudentTest
    while(!answer.equals("3"));

    I would like some help with fixing or changing how to add to my array for now
    and perhaps someone to steer me in the right direction for the editing part later.
    As i have commented in the code i know i'd maybe need some kind of array index selection but i have no idea how to implement that.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    I've just realised my mistake with the Keyboard input, i moved my files but not the keyboardinput class file. but the rest is still giving me problems

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    I hope your code is not even compiling, the reason is, What is KeyboardInput?
    There is no class in java like this. Did you make your own KeyboardInput class? If yes, where it is?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    the rest is still giving me problems
    Please copy and paste here the full text of the error messages.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    Hi again, I've changed alot of the code and it compiles but is now very basic.

    KeyboardInput is a class I borrowed from a lecturer at uni which i think he created himself. Anyways that works.
    I would like some suggestions of how to let a user enter a new student.
    In the if statement, condition 1. How would I go about getting user inputs to be 'stored' into kid[1] then later if another new student was added kid[2]?

    public class StudentTest 
    {
      public static void main(String[] args) 
    {
    int id;
    String n;
    String d; 
    String t; 
    String h;
    String c;
    int f; 
    String cr;
    int m; 
    String p;
     
        Student[] kid;
        kid = new Student[3];
        int i = 0;
     
       id = 1;
       n = "Grant Searle";
       d = "09/12/1991";
       t = "Same as Home Address";
       h = "5 James Road, Gosport, Hampshire, PO13 0TF";
       c = "Computer Network Management & Design";
       f = 3275;
       cr = "Mother";
       m = 56;
       p = "Yes";
     
       Student startingKid = new Student( id, n, d, t, h, c, f, cr, m, p ) ;
       kid[i] = startingKid;
     
     
    System.out.println("What would you like to do?");
    System.out.println("1) Add a student record");
    System.out.println("2) Edit an existing record");
    System.out.println("3) Delete a student record");
    System.out.println("4) Display current records");
     
     
    KeyboardInput in = new KeyboardInput() ;
    int x = in.readInteger() ;
     
    if (x == 1)
     
    System.out.println("Please Enter Student's Full Name:");
     
     
    else if (x==2)
    System.out.println("Edit");
     
    else if (x==3)
    System.out.println("Delete");
     
    else if (x==4)
    for (i = 0; i < kid.length; i++)
    System.out.println(kid[i] + "\n");
     
    else
    System.out.println("do nothing");	
     
      }
    }

    Any pointers or code examples would help alot

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [Help] Problem with Class arrays, adding/editing and deleting

    KeyboardInput is a class I borrowed from a lecturer
    Your code should have an import statement if you are using classes from a third party.
    how to let a user enter a new student.
    Ask the user for the data,
    read the data from the user into a variable.
    Continue asking the user for data until you have all the needed data.
    Create the new student using the data that was saved above in the variables where it was read from the user.
    Add that new student object to the array and increment the index to the array to the next empty slot in the array.

Similar Threads

  1. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 07:23 AM
  2. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 07:28 AM
  3. Adding data to a text box on another class
    By Qualitystreet in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2011, 02:38 AM
  4. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 08:29 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM