473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Input from user in arraylist

Hi friends
I m very new for java and i m reading collections right now. I m writing data into a text file and then transfering it in an arraylist. I want the reverse of it, which i am trying n hopefully i will get the soln also but the main problem is that i want to enter the data from user into an array list which will be shown in the text file and vice versa.. Plz help me

This i have done so far..

package comp;

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.util.ArrayList;
  11.  
  12. public class ProcessStudent {
  13.  
  14.  
  15.     public static void main(String[] args){
  16.         ArrayList<Student> studentList=new ArrayList<Student>();
  17.         BufferedReader max;
  18.  
  19.         Student student1=new Student("Kamal","Mindtree",25);
  20.                    Student student2=new Student("Smitesh","Accenture",23);
  21.         Student student3=new Student("Sudish","Oracle",29);
  22.  
  23.         studentList.add(student1);
  24.         studentList.add(student2);
  25.         studentList.add(student3);
  26.  
  27.         File file=new File("C:/std1.txt");
  28.         FileOutputStream fos=null;
  29.         FileInputStream fis=null;
  30.         ObjectOutputStream oos=null;
  31.         ObjectInputStream ois=null;
  32.  
  33.         try{             
  34.             fos=new FileOutputStream(file);
  35.             oos=new ObjectOutputStream(fos);
  36.             oos.writeObject(studentList);
  37.  
  38.             fis=new FileInputStream(file);
  39.             ois=new ObjectInputStream(fis);
  40.             Object output=ois.readObject();
  41.  
  42.             ArrayList<Student> studentListDuplicate =(ArrayList) output;
  43.             for(Student student: studentListDuplicate){
  44.                 System.out.println(student.toString());
  45.             }
  46.         }catch (IOException e) {
  47.             e.printStackTrace(); 
  48.         }catch(ClassNotFoundException c){
  49.             c.printStackTrace();
  50.         }
  51.         finally{
  52.             try{
  53.                 if(fos!=null){
  54.                     fos.close();
  55.                 }
  56.                 if(fis!=null){
  57.                     fis.close();
  58.                 }
  59.                 if(oos!=null){
  60.                     oos.close();
  61.                 }
  62.                 if(ois!=null){
  63.                     ois.close();
  64.                 }
  65.             }catch(IOException e){
  66.                 e.printStackTrace();
  67.             }
  68.         }
  69.     }
  70. }
Sep 6 '07 #1
4 3888
madhoriya22
252 100+
Hi friends
I m very new for java and i m reading collections right now. I m writing data into a text file and then transfering it in an arraylist. I want the reverse of it, which i am trying n hopefully i will get the soln also but the main problem is that i want to enter the data from user into an array list which will be shown in the text file and vice versa.. Plz help me

This i have done so far..
Hi,
Always use code tags while posting code. Luk how nice ur code is luking now ...
Expand|Select|Wrap|Line Numbers
  1. package comp;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.ObjectInputStream;
  11. import java.io.ObjectOutputStream;
  12. import java.util.ArrayList;
  13.  
  14. public class ProcessStudent {
  15.  
  16.  
  17.     public static void main(String[] args){
  18.         ArrayList<Student> studentList=new ArrayList<Student>();
  19.         BufferedReader max;
  20.  
  21.         Student student1=new Student("Kamal","Mindtree",25);
  22.      Student student2=new Student("Smitesh","Accenture",23);
  23.         Student student3=new Student("Sudish","Oracle",29);
  24.  
  25.         studentList.add(student1);
  26.         studentList.add(student2);
  27.         studentList.add(student3);
  28.  
  29.         File file=new File("C:/std1.txt");
  30.         FileOutputStream fos=null;
  31.         FileInputStream fis=null;
  32.         ObjectOutputStream oos=null;
  33.         ObjectInputStream ois=null;
  34.  
  35.         try{             
  36.             fos=new FileOutputStream(file);
  37.             oos=new ObjectOutputStream(fos);
  38.             oos.writeObject(studentList);
  39.  
  40.             fis=new FileInputStream(file);
  41.             ois=new ObjectInputStream(fis);
  42.             Object output=ois.readObject();
  43.  
  44.             ArrayList<Student> studentListDuplicate =(ArrayList) output;
  45.             for(Student student: studentListDuplicate){
  46.                 System.out.println(student.toString());
  47.             }
  48.         }catch (IOException e) {
  49.             e.printStackTrace(); 
  50.         }catch(ClassNotFoundException c){
  51.             c.printStackTrace();
  52.         }
  53.         finally{
  54.             try{
  55.                 if(fos!=null){
  56.                     fos.close();
  57.                 }
  58.                 if(fis!=null){
  59.                     fis.close();
  60.                 }
  61.                 if(oos!=null){
  62.                     oos.close();
  63.                 }
  64.                 if(ois!=null){
  65.                     ois.close();
  66.                 }
  67.             }catch(IOException e){
  68.                 e.printStackTrace();
  69.             }
  70.         }
  71.     }
  72. }
What the problem u are having in this code. I think U need to serialize this class.
Sep 6 '07 #2
r035198x
13,262 8TB
Hi friends
I m very new for java and i m reading collections right now. I m writing data into a text file and then transfering it in an arraylist. I want the reverse of it, which i am trying n hopefully i will get the soln also but the main problem is that i want to enter the data from user into an array list which will be shown in the text file and vice versa.. Plz help me

This i have done so far..

package comp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ProcessStudent {


public static void main(String[] args){
ArrayList<Student> studentList=new ArrayList<Student>();
BufferedReader max;

Student student1=new Student("Kamal","Mindtree",25);
Student student2=new Student("Smitesh","Accenture",23);
Student student3=new Student("Sudish","Oracle",29);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

File file=new File("C:/std1.txt");
FileOutputStream fos=null;
FileInputStream fis=null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;

try{
fos=new FileOutputStream(file);
oos=new ObjectOutputStream(fos);
oos.writeObject(studentList);

fis=new FileInputStream(file);
ois=new ObjectInputStream(fis);
Object output=ois.readObject();

ArrayList<Student> studentListDuplicate =(ArrayList) output;
for(Student student: studentListDuplicate){
System.out.println(student.toString());
}
}catch (IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException c){
c.printStackTrace();
}
finally{
try{
if(fos!=null){
fos.close();
}
if(fis!=null){
fis.close();
}
if(oos!=null){
oos.close();
}
if(ois!=null){
ois.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
1.) Please use code tags when posting code.
2.) The code you posted has nothing to do with what you described above.
Sep 6 '07 #3
Nepomuk
3,112 Expert 2GB
2.) The code you posted has nothing to do with what you described above.
I don't agree r035198x, it might well have to do with one problems described.

You want to read in data for an ArrayList? Do you mean the studentList?

Anyhow, first of all, create a function, which has the Data, you would like to enter manually later, creates whatever you want to add (e.g. a Student object) and then adds the new Data to you list. When you have that working, have a look at the "java.util.Scanner" class. That lets you read in data and work with it.

Greetings,
Nepomuk
Sep 6 '07 #4
r035198x
13,262 8TB
I don't agree r035198x, it might well have to do with one problems described.

You want to read in data for an ArrayList? Do you mean the studentList?

Anyhow, first of all, create a function, which has the Data, you would like to enter manually later, creates whatever you want to add (e.g. a Student object) and then adds the new Data to you list. When you have that working, have a look at the "java.util.Scanner" class. That lets you read in data and work with it.

Greetings,
Nepomuk
I expected the OP to post an attempt at reading data from the user.

@OP If you want the data to be human readable in the file then you need to write Strings to the file not serialized objects.
Sep 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jay Douglas | last post by:
I need help getting the collections to show up in the property window of a user control. I want to be able to edit the collection items just like any other asp.net control that has collection...
0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
1
by: Jay Douglas | last post by:
I need help getting the collections to show up in the property window of a user control. I want to be able to edit the collection items just like any other asp.net control that has collection...
2
by: Cache123 | last post by:
This problem is dealing with array lists. I already created an empty array list of temperatures....How would i go about asking the user if they want to enter daily high and low temperatures. if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.