Results 1 to 7 of 7
Thread: Beginner Problems
- 12-12-2011, 11:14 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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:
At the moment my program simply prints the first string I enter, but I'd like something more advanced,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)); } }
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
- 12-12-2011, 12:08 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2011, 10:53 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
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
- 12-16-2011, 11:07 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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:
and the second is the one i'm having problems withJava 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(); } }
I know i've made mistakes and my syntax is probably horrid but if anyone could help me out i'd really appreciate it.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 } } } }
Thanks
- 12-16-2011, 11:10 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Beginner Problems
also noticed i have declared inName etc twice :/
- 12-16-2011, 12:51 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
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
- 12-16-2011, 01:38 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
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:
Now my question is, how in if x==1 do i save user input to the kid array,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"); } }
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
-
Need help, beginner here
By fuyongwu in forum New To JavaReplies: 21Last Post: 10-02-2011, 02:26 PM -
beginner
By Eiolvit in forum New To JavaReplies: 4Last Post: 07-10-2011, 05:32 PM -
JNI beginner
By M77 in forum Advanced JavaReplies: 2Last Post: 04-04-2011, 12:53 AM -
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
Need Help - Beginner
By ooooohmaul in forum New To JavaReplies: 4Last Post: 08-08-2010, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks