[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:
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();
}
}
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 :/
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]);
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
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.
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.
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
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?
Re: [Help] Problem with Class arrays, adding/editing and deleting
Quote:
the rest is still giving me problems
Please copy and paste here the full text of the error messages.
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]?
Code :
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 :)
Re: [Help] Problem with Class arrays, adding/editing and deleting
Quote:
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.
Quote:
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.