Results 1 to 8 of 8
  1. #1
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Problem with arrays

    Hello.

    I'm new to this forum and very new to programming in Java. I just started with arrays and now I have an exercise I need to complete.

    The goal here is to create an array of student's cvs and then:

    -insert a student ordered by name
    -list all cvs ordered by name
    -delete cvs with a name between (ex:)a and c
    -search for a cv, given the name and the course
    -list cvs by a given course


    This is what I have so far, but neither it complies, nor I know what to do next.

    Any ideas, please?

    Thank you.


    _______________________________________________

    public class Main {
    public static int Choices(){
    int chooseOption;
    do{
    String menu = "Menu"
    + "\n1 - Insert a CV"
    + "\n2 - Delete CV"
    + "\n3 - List CV Asc By Name"
    + "\n4 - Search CV by Name and Course"
    + "\n5 - List CV por Course"
    + "\n0 - Exit";
    chooseOption = Integer.parseInt(JOptionPane.showInputDialog(menu + "\nOption? (0 to 5)"));
    }
    while ((chooseOption < 0) || (chooseOption > 5));
    return chooseOption;
    }

    static int size;

    public static void main(String[] args) {

    do {
    size = Integer.parseInt(JOptionPane.showInputDialog("Size of the array:"));
    if(size <= 0)
    JOptionPane.showMessageDialog(null,"ERROR! Must be positive!");
    }

    while(size <= 0);
    Vector v = new Vector(size);
    int option = Choices();

    while (option != 0){
    switch (option){
    case 1: {
    if (v.isFull() == true)
    JOptionPane.showMessageDialog(null,"Vector full!");
    else {
    String name = JOptionPane.showInputDialog("name: ");
    String address = JOptionPane.showInputDialog("address: ");
    String email = JOptionPane.showInputDialog("Email: ");
    String course = JOptionPane.showInputDialog("Course: ");
    int grade = Integer.parseInt(JOptionPane.showInputDialog("Grad e: "));


    v.add(name, address, email, course, grade);
    }
    break;
    }


    case 2: {
    if (v.isEmpty() == true)
    JOptionPane.showMessageDialog(null,"Nothing to elimitate.");
    else {
    String name = JOptionPane.showInputDialog("Name: ");
    v.delete(name);
    }
    break;
    }

    default:
    System.out.println("Wrong option");
    }
    option = Choices();
    }
    }
    }



    __________________________________________________ _

    public class Student {
    private String name;
    private String address;
    private String email;
    private String course;
    private int grade;

    public Student(String name, String address, String email, String course, int grade) {
    this.name = name;
    this.address = address;
    this.email = email;
    this.course = course;
    this.grade = grade;
    }

    public String getname() {
    return name;
    }

    public void setname(String name) {
    this.name = name;
    }

    public String getaddress() {
    return address;
    }

    public void setaddress(String address) {
    this.address = address;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getcourse() {
    return course;
    }

    public void setcourse(String course) {
    this.course = course;
    }

    public float getgrade() {
    return grade;
    }

    public void setgrade(int grade) {
    this.grade = grade;
    }

    public String toString() {
    return name + " lives in " + address + " with the email " + email + " graduated in " + course + " with " + grade;
    }
    }


    __________________________________________________ __



    public class Array {
    private Student[] listStudents;
    private int i;
    private int maxElements;

    public Array(int size) {
    listStudents = new Student[size];
    maxElements = size;
    i = -1;
    }
    // verifies if full
    public boolean isFull() {
    if (i + 1 == maxElements)
    return true;
    else
    return false;
    }
    // verifies if empty
    public boolean isEmpty() {
    if (i == -1)
    return true;
    else
    return false;
    }

    // add to Array
    public void add(String name, String address, String email, String course, int grade) {
    Student new = new Student(name, address, email, course, grade);
    i++;
    listStudents[i] = new;
    }

    // removes element from Array
    public void remove(String name) {
    for (int i=0 ; i < listStudents.length ; i++){
    if (listStudents[i].equals(name)){
    listStudents.remove[i];
    }
    else{
    System.out.println ("The name does not exist");
    }
    }
    }

    // know how many elements on the Array
    public int getPosition() {
    return i;
    }
    // Print all
    public void print() {
    for(i = 0 ; i < listStudents.length ; i++){
    System.out.println(listStudents[i]);
    }
    }
    // search Student on Array
    public void search(String name) {
    for (int i=0 ; i < listStudents.length ; i++){
    if (listStudents[i].equals(name)){
    System.out.println(listStudents[i]);
    }
    else{
    System.out.println ("Name doesn't exist");
    }
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Viper View Post
    but neither it complies
    Care to tell us what the compiler told you?

    kind regards,

    Jos

  3. #3
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    if (listStudents[i].equals(name)){
    listStudents.remove[i];


    It says here that "Cannot invoke remove(String) on the array type student"

    Thank you for answering.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Cannot invoke remove(String) on the array type student
    listStudents is an array. Arrays don't have methods.
    You need a method that you pass the array to that removes the element from the array.

    Why does the error message show String as the argument to the remove() method?
    The statement shows an array reference:
    listStudents.remove[i];

    Is the posted code the same as the code that generated the error message?
    Last edited by Norm; 10-05-2010 at 09:53 PM.

  5. #5
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Hello.

    You are correct, translation problem. When I translated it, I changed "name" to i.

    So, with name, I have the error I posted.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Please post the line of code that has the error.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I am a man who likes to see others help themselves but even the best of us need assistance from time to time.

    There are better ways to acheive the results you are looking for I shall stick with the basics for now.

    As previously mentioned, fixed arrays contains very little in terms of functionality so any intent on adding or removing elements must be handled by the programmer. As the array is of fixed size there is no need to adjust the actual size of the array so this simplifies the process.

    Instead of calling the method "remove" (which does not exist), what you will need to do is use an iterative loop to cause the elements after the one you wish to remove to decrement their position by one place.

    e.g. listStudents[i] = listStudents[i+1];

    This method will not remove the element you wish to but instead overwrites it, effectively the same result.

    Remember though that when the elements are shifted the last element may retain its previous assignment (depending on your code of course). In this case, simply set it to null.

    There are a couple of other issues within the code, but I will give you the chance to help yourself before stepping in.

    Any further issues then please post back.

  8. #8
    Viper is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Hello, Ronin.

    Thank you for your help. That did the trick. I will now try to write the code for the other options and I'll post if I need further help.

    Regards.

Similar Threads

  1. Arrays.asList(...) Problem
    By plm-pusik in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 01:12 AM
  2. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  3. loop problem-arrays
    By ester in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 09:44 PM
  4. Mergin two arrays problem.
    By frasifrasi in forum New To Java
    Replies: 4
    Last Post: 07-09-2008, 03:29 PM
  5. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 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
  •