Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am a newbie in Java, I need help in using Vector class in this assignment
import javax.swing.*;
public class Main
{
public static void main (String[] args)
{
Info studentinfo[]= new Info[5];
int counter=0;
int option;
String more="";
String add="";
String ss="";
String name="";
String g1,g2,g3,g4,g5,input;int gr[]=new int[5];
for (int y=0;y<5;y++)
{
studentinfo[y]=new Info();
}
do // starting menu //
{
input=JOptionPane.showInputDialog(null, "1.Enter student info" + "\n"
+ "2.Enter Course grades" + "\n"
+ "3. View Total and Average" + "\n"
+ "4. View GPA" + "\n"
+ "5. Summary" + "\n"
+ "6. Exit" + "\n", "Choose option", JOptionPane.PLAIN_MESSAGE);
option=Integer.parseInt(input);
switch(option) // menu options //
{
case 1:if (counter<=5)
{do{
int index;
boolean find=false;
ss=JOptionPane.showInputDialog("Enter Social Security"); // checking for Social security //
for (int i=0;i{
if ((studentinfo[i].SS).equals(ss))
{
find=true;
index =i;
break;
}
}
if (!find) // If SS found, then continue else prompt user
{
name=JOptionPane.showInputDialog("Enter your name");
add= JOptionPane.showInputDialog("Enter your address");
studentinfo[counter].setName(name); // getting student information //
studentinfo[counter].setAddress(add);
studentinfo[counter].setSS(ss);
counter++;
}
else
JOptionPane.showMessageDialog(null, "Social security exists,Try again","Error", JOptionPane.PLAIN_MESSAGE);
more=JOptionPane.showInputDialog("Do you want more(y/n)");
}
while(((more.equals("Y"))||(more.equals("y"))) &&(counter<5)); // continue 5 more times for 5 students
}
break;
case 2:
int index=0;
boolean find=false;
if (counter<1)
{
JOptionPane.showMessageDialog(null, "Database Empty", "Choose again", JOptionPane.ERROR_MESSAGE); // check to see if information exits //
}
else
{
ss=JOptionPane.showInputDialog("Enter Social Security"); // checking for SS, if exists, continue, else prompted //
for (int i=0;i{
if ((studentinfo[i].SS).equals(ss))
{
find=true;
index =i;
break;
}
}
if (find)
{
g1= JOptionPane.showInputDialog("Enter marks for Course 1");
g2= JOptionPane.showInputDialog("Enter marks for Course 2");
g3= JOptionPane.showInputDialog("Enter marks for Course 3");
g4= JOptionPane.showInputDialog("Enter marks for Course 4");
g5= JOptionPane.showInputDialog("Enter marks for Course 5");
gr[0]= Integer.parseInt(g1); // getting the course marks//
gr[1]= Integer.parseInt(g2);
gr[2]= Integer.parseInt(g3);
gr[3]= Integer.parseInt(g4);
gr[4]= Integer.parseInt(g5);
studentinfo[index].setGrades(gr);
}
else
JOptionPane.showMessageDialog(null, "Social security doesnt exist, Try again ","Error", JOptionPane.PLAIN_MESSAGE);
}
break;
case 3:index=0;
find=false;
if (counter<1)
{
JOptionPane.showMessageDialog(null, "Database Empty", "Choose again", JOptionPane.ERROR_MESSAGE); // checking if information exists//
}
else
{
ss=JOptionPane.showInputDialog("Enter Social Security");
for (int i=0;i{
if ((studentinfo[i].SS).equals(ss))
{
find=true;
index =i;
break;
}
}
if (find)
{
JOptionPane.showMessageDialog(null, "Name=" +studentinfo[index].name+"\n" + "Total" +" "+ studentinfo[index].tot+"/"+"500"+"\n" +" "+ "Average=" +studentinfo[index].ave+"\n", "Arif's little helper", JOptionPane.PLAIN_MESSAGE); // displaying info //
}
else
JOptionPane.showMessageDialog(null, "Social security doesnt exist, Try again", "Error",JOptionPane.PLAIN_MESSAGE); // displaying info //
}
break;
case 4:
index=0;
find=false;
if (counter<1)
{
JOptionPane.showMessageDialog(null, "Database Empty", "Choose again", JOptionPane.ERROR_MESSAGE);
}
else
{
ss=JOptionPane.showInputDialog("Enter Social Security"); // checking for SS, if exists, continue, else prompted //
for (int i=0;i{
if ((studentinfo[i].SS).equals(ss))
{
find=true;
index =i;
break;
}
}
if (find)
{
JOptionPane.showMessageDialog(null, "Name=" +studentinfo[index].name+"\n" + "GPA=" + studentinfo[index].GPA + "\n" // displaiyng gpa//
, "Arif's little helper", JOptionPane.PLAIN_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "Social security doesnt exist, try again","Error", JOptionPane.PLAIN_MESSAGE);
}
break;
case 5:
if (counter<1)
{
JOptionPane.showMessageDialog(null, "Database Empty", "Choose again", JOptionPane.ERROR_MESSAGE);
}
else
{
for (int x=0;x{
JOptionPane.showMessageDialog(null, "Name=" + studentinfo[x].name // showing summary //
+ "\n" + "Social Security=" + studentinfo[x].SS
+ "\n" + "Address=" + studentinfo[x].address
+ "\n" + "\n" + "\n"
+ "SUMMARY"
+ "\n"
+ "Total=" + studentinfo[x].tot + "/500"
+ "\n" + "Average=" + studentinfo[x].ave
+ "\n" + "GPA=" + studentinfo[x].GPA
+ "\n" + "Your grade is" + " " + studentinfo[x].Grade , "Arif's little helper", JOptionPane.PLAIN_MESSAGE);
}
}
break;
case 6: //exiting program//
JOptionPane.showMessageDialog(null, "Thanks for using this program", "Bye", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Selection error", "Choose again", JOptionPane.ERROR_MESSAGE);
break;}
}
while (option!=6); // end menu//
}
}
I also have a class of Info.Now instead of the arrays i need to use vectors so i can have unlimited students, can anyone help. Thanks

Umm...if you're using JDK 1.3, I think you'll need to use ArrayLists, not Vectors. Aren't vectors fossils now?

The main difference between vector and arrays besides the dynamic size is that vector can store any data types. What this means for you is that you have to type cast the data when you retrieve from the vector.
For more info I advise you to read the java docs for methods.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |