Java program to insert records using arraylist.

Rits rishi
@rits-rishi-VsVG1W โ€ข Oct 10, 2024
import java.util.*;
class employee
{
emp e1=new emp();
static int limit;
static Scanner s1=new Scanner(System.in);
ArrayList<emp>  my_list=new ArrayList<emp>();
public static void main(String[] args)
{
System.out.println("WELCOME");
System.out.print("Enter the limit=");
limit=s1.nextInt();
employee em=new employee();
for(int i=0;i<limit;i++)
{
em.input();
}
em.output();
em.menu();
}
public int menu()
{
System.out.println("\n Menu \n 1-insert \n 2-Delete \n 3-Display\n4-Exit");
System.out.print("Enter your option[1-4]=");
int op=s1.nextInt();
return op;
 
}
public void input()
{
System.out.println("Enter Empname=");
e1.empname=s1.next();
System.out.println("Enter Empid=");
e1.empid=s1.nextInt();
System.out.println("Enter Empsalary=");
e1.empsalary=s1.nextFloat();
my_list.add(e1);
 
}
 
public void output()
{
 
System.out.println("Empid\tEmpname\tEmpsalary");
emp temp=new emp();
for(int j=1;j<limit;j++)
{
temp=my_list.get(j);
System.out.println(temp.empid+"\t"+temp.empname+"\t"+temp.empsalary);
} 
 
  }
 
 
}
 
 
class emp
{
public int empid;
public String empname;
public float empsalary;
}

//this program does not print different records as entered according to the limit.it displays only the value last entered. example- if i enter 1st record- riti 1 2900
enter 2nd record-abc 2 1200
then it displays-
abc 2 1200
abc 2 1200
kindly help me.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anand Tamariya

    @anand-tamariya-DnfjEX • Jul 23, 2013

    Add following at the start of input():

    e1 = new emp();
  • Rits rishi

    @rits-rishi-VsVG1W • Jul 25, 2013

    Anand TamariyaAdd following at the start of input():
    e1 = new emp();

    i added your statment..but now it's displaying only 1 record & that too last one.

  • rahul69

    @rahul69-97fAOs • Jul 25, 2013

    Rits rishii added your statment..but now it's displaying only 1 record & that too last one.

    Now, in output function, instead of :

    int j=1

    inside the for loop, write

    int j=0

    ๐Ÿ˜€

  • Rits rishi

    @rits-rishi-VsVG1W • Jul 25, 2013

    rahul69Now, in output function, instead of :

    inside the for loop, write

    ๐Ÿ˜€

    THANK YOU SO MUCH.....๐Ÿ‘๐Ÿ˜๐Ÿ˜€

  • Rits rishi

    @rits-rishi-VsVG1W • Jul 25, 2013

    rahul69Now, in output function, instead of :

    inside the for loop, write

    ๐Ÿ˜€

    THANK YOU SO MUCH...๐Ÿ˜€ ๐Ÿ‘๐Ÿ˜€

  • rahul69

    @rahul69-97fAOs • Jul 25, 2013

    Rits rishiTHANK YOU SO MUCH...๐Ÿ˜€ ๐Ÿ‘๐Ÿ˜€

    Ur Welcome ๐Ÿ˜’

  • simplycoder

    @simplycoder-NsBEdD • Jul 25, 2013

    #-Link-Snipped-# : I would advise you to get your concepts cleared in this.
    Knowing how arraylist works and why you have to create a new instance every time to add into it would be of a great help.

  • Rits rishi

    @rits-rishi-VsVG1W • Jul 25, 2013

    simplycoder#-Link-Snipped-# : I would advise you to get your concepts cleared in this.
    Knowing how arraylist works and why you have to create a new instance every time to add into it would be of a great help.

    i know..n i am trying my best to achieve perfection..can u give me some programs regarding arraylist that i can practice.??๐Ÿ˜ i'll post my answers here..it would be great help from your side..as i am having test on monday & topic is "arraylist".
    please start from easy ones to complex ones..so that i can build up my confidence..๐Ÿ˜€

  • simplycoder

    @simplycoder-NsBEdD • Jul 26, 2013

    Rits rishii know..n i am trying my best to achieve perfection..can u give me some programs regarding arraylist that i can practice.??๐Ÿ˜ i'll post my answers here..it would be great help from your side..as i am having test on monday & topic is "arraylist".
    please start from easy ones to complex ones..so that i can build up my confidence..๐Ÿ˜€

    Ok,
    1)WAP which will take input from user of n integers and print it.
    2)WAP which will print the above list in ascending and descending order.
    3)WAP which will take the input as a string arraylist.
    "I love CE . I am proud to be a CEan ."
    Print the count first.
    Remove '.' character from the above sentence and print the list and the count.
    4)Convert all the characters to lower case and upper case from the above question.
    5)Reverse the sentence along but donot change the order of the alphabets in the word.
    (exected output : . CEan a be to proud am I . CE love I).
    6**)Calculate 100! using arrayList.

    first five are pretty simple. Don't be demoralized if you don't get the 6th one