Results 1 to 8 of 8
Thread: Problem with arrays
- 10-05-2010, 01:08 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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");
}
}
}
}
- 10-05-2010, 01:24 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-05-2010, 09:23 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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.
- 10-05-2010, 09:50 PM #4
listStudents is an array. Arrays don't have methods.Cannot invoke remove(String) on the array type student
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.
- 10-06-2010, 12:24 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Hello.
You are correct, translation problem. When I translated it, I changed "name" to i.
So, with name, I have the error I posted.
- 10-06-2010, 02:07 PM #6
Please post the line of code that has the error.
- 10-06-2010, 08:14 PM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 10-07-2010, 02:49 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Arrays.asList(...) Problem
By plm-pusik in forum New To JavaReplies: 2Last Post: 09-18-2010, 01:12 AM -
Problem: Arrays and Sorting
By Rhez in forum New To JavaReplies: 7Last Post: 02-03-2010, 02:18 PM -
loop problem-arrays
By ester in forum New To JavaReplies: 0Last Post: 02-02-2010, 09:44 PM -
Mergin two arrays problem.
By frasifrasi in forum New To JavaReplies: 4Last Post: 07-09-2008, 03:29 PM -
Arrays Problem (Advanced Java...Need Help)
By Zebra in forum New To JavaReplies: 9Last Post: 05-02-2008, 01:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks