Results 1 to 7 of 7
  1. #1
    Grant_Searle is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Beginner Problems

    Hello java-forums,
    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 to be able to add new 'records' and edit or delete them once created.
    From reading various online tutorials, I know this will require an array or two for my 'objects' and a fair few if statements and methods but I have no idea how to expand on the program I have already, which is this:
    Java Code:
    import java.util.*;
    
    public class UserInput {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            Scanner stdin = new Scanner(System.in);
    
            do {
                System.out.println("Current list of Students is: " + list);
                System.out.println("Add more? (y/n)");
                if (stdin.next().startsWith("y")) {
                    System.out.println("Enter New Details: ");
                    list.add(stdin.next());
                } else {
                    break;
                }
            } while (true);
    
            System.out.println("Student Information is now: " + list);
            String[] arr = list.toArray(new String[0]);
            System.out.println("Array is " + Arrays.toString(arr));
        }
    }
    At the moment my program simply prints the first string I enter, but I'd like something more advanced,
    I was thinking about adding something like this:
    Person[] kid = new Person[];
    kid[0] = new Person("Grant Searle", "09/12/1991", "Address..." );
    kid[1] = new Person("John Doe", "etc");
    kid[2] = new Person("Tony Tester", "etc");

    Therefore there would be data in the program already but a 'user' could always add or edit more.

    If any one could point me in the right direction that would be much appreciated.

    Thank you

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Beginner Problems

    As you already wrote yourself: design and implement a Person class that store a name, dob, address etc. per person. For now implement a constructor that takes those String values as parameters. For additional functionality you can make your Person class implement the Comparable<Person> interface so you can store Person objects in a SortedSet or sort a List of Persons; make sure you also implement the equals( ... ) method as well as the hashCode() method. The API documentation will tell you all about the details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Beginner Problems

    Is your problem how it only displays one result yet you want it to display all? if so try making a loop statment to retrieve all the data and break down the arrray list into colummns such as name age time etc

  4. #4
    Grant_Searle is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Beginner Problems

    Hi again, thank you both for your replies.
    I've been playing with the code for a few days now, i know WHAT i want to do and the WAY in which it is done
    but... i dont know HOW to go about writing it

    I've kinda been writing what bits i know of the code and as a result there is holes everywhere and i get errors.

    I now have 2 files:
    Java Code:
    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();
    }
    }
    and the second is the one i'm having problems with

    Java Code:
    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]);
    
    
    KeyboardInput in = new KeyboardInput() ;
    
    String inName; String inDate; String inTerm; String inHome; String inCourse; int inFees; String inRela; int inMarks; String inProg;
    
    inName = n;
    inDate = d;
    inTerm = t;
    inHome = h;
    inCourse = c;
    inFees = f;
    inRela = cr;
    inMarks = m;
    inProg = p;
    
     kid[1] = new Student (n, d, t, h, c, f, cr, m, p);
    
    
    do {
       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
    }
    
      }
    }
    }
    I know i've made mistakes and my syntax is probably horrid but if anyone could help me out i'd really appreciate it.

    Thanks

  5. #5
    Grant_Searle is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Beginner Problems

    also noticed i have declared inName etc twice :/

  6. #6
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Beginner Problems

    Hi Grant,

    I can see a few problems in your code.


    First, im guessing that you've created the Student[] to store more than 1 student. By saying " kid = new Student[1];" at line 16 you have created a Student[] that can take only 1 Student object. And at line 51 you have created a second Student object for a Student[] that can only take 1 element. This would cause an error because you cannot grow an array like this. If you want to store the student object i would recommend looking into either the ArrayList class or the HashMap class.

    Second, at line 34 you have said "System.out.println(kid[i]);". When you do this what you are printing is a memory address for the Student object at kid[i]. Try converting it to a string first.

    GoodLuck

  7. #7
    Grant_Searle is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Beginner Problems

    Hey again, I've been playing some more and made my code much tidier and it compiles now.

    here is what i've changed it to:

    Java Code:
    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[5];
        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;
    
    
    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");	
    	
      }
    }
    Now my question is, how in if x==1 do i save user input to the kid array,
    I've tried:
    System.out.println("Please Enter Student's Full Name:");
    String inputName = in.readString() ;

    However the error i get is else without if, I've tried removing certain ; but same error.
    How can it work without String inputName = in.readString() ; but not with?

Similar Threads

  1. Need help, beginner here
    By fuyongwu in forum New To Java
    Replies: 21
    Last Post: 10-02-2011, 03:26 PM
  2. beginner
    By Eiolvit in forum New To Java
    Replies: 4
    Last Post: 07-10-2011, 06:32 PM
  3. JNI beginner
    By M77 in forum Advanced Java
    Replies: 2
    Last Post: 04-04-2011, 01:53 AM
  4. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  5. Need Help - Beginner
    By ooooohmaul in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 05:22 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
  •