Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Arraylist issue: pass all the arrayList `s object to other arrayList ...

807606May 28 2007 — edited May 28 2007
hi all...i hope somebody could show me some direction on my problem...i want to pass a arraylist `s cd information to an other arraylist
and save the new arraylist `s object to a file...i try to solve for a long ..pls help...
import java.text.*;
import java.util.*;
import java.io.*;

public class Demo{
	readOperation theRo = new readOperation();
	errorCheckingOperation theEco = new errorCheckingOperation();
	ArrayList<MusicCd>  MusicCdList;
	private void heading()
	{
		System.out.println("\tTesting read data from console, save to file, reopen that file\t");
	}
	
	private void readDataFromConsole()
	//private void insertCd()
	{ 
	   MusicCdList = new ArrayList<MusicCd>( );  
	   MusicCd theCd;
	   int muiseCdsYearOfRelease;
	   int validMuiseCdsYearOfRelease;
	   String muiseCdsTitle;
		while(true)
		{
			
			 String continueInsertCd = "Y";
		        
			do
			{
				
				muiseCdsTitle = theRo.readString("Please enter your CD`s title : ");
				muiseCdsYearOfRelease = theRo.readInt("Please enter your CD`s year of release : ");
				validMuiseCdsYearOfRelease = theEco.errorCheckingInteger(muiseCdsYearOfRelease, 1000, 9999);
				MusicCdList.add(new MusicCd(muiseCdsTitle, validMuiseCdsYearOfRelease));//i try add the cd`s information to the arrayList
				MusicCdList.trimToSize();
				//saveToFile(MusicCdList);
				continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");
					
			}while(continueInsertCd.equals("Y") || continueInsertCd.equals("y") );
			
			if(continueInsertCd.equals("N") || continueInsertCd.equals("n"));
			{           
                                                //MusicCdList.add(new MusicCd(muiseCdsTitle, muiseCdsYearOfRelease));						
				break;
			}
		        //System.out.println("You `ve an invalid input " + continueInsertCd + " Please enter (Y/N) only!!");
			
		}
	}
	
	//i want to pass those information that i just add to the arrayList to file
	//I am going to pass the arraylist that contains my cd`s information to new arraylist "saveitems and save it to a file...
	//i stuck on this problem
	//how do i pass all the arrayList `s object to another arraylist ..pls help
	//it is better show me some example how to solve thx a lot
	private void saveToFile(ArrayList<MusicCd> tempItems)
	{
		ArrayList<MusicCd> saveItems;
 
	
		saveItems = new ArrayList<MusicCd>();
		try 
		{
			File f = new File("cdData.txt");
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
 
			saveItems.add(ArrayList<MusicCd> tempItems);
			//items.add("Second item.");
			//items.add("Third item.");
			//items.add("Blah Blah.");
 
			oos.writeObject(items);
			oos.close();
		} 
		catch (IOException ioe) 
		{ 
			ioe.printStackTrace(); 
		}
 
		try 
		{
			File g = new File("test.fil");
			FileInputStream fis = new FileInputStream(g);
			ObjectInputStream ois = new ObjectInputStream(fis);
 
			ArrayList<String> stuff = (ArrayList<String>)ois.readObject();
			for( String s : stuff ) System.out.println(s);
 
			ois.close();
 
		} 
		catch (Exception ioe)
		{ 
			ioe.printStackTrace(); 
		}
 
	}
 

	public static void main(String[] args)
	{
		Demo one = new Demo();
		one.readDataFromConsole();
		//one.saveToFile();
		
	}
	
}
		
//the followring code for better understang
import java.io.Serializable;

public class MusicCd implements Serializable
{
	
	private String musicCdsTitle;
        private int yearOfRelease;
	public MusicCd()
	{
		musicCdsTitle = "";
		yearOfRelease = 1000;
	}

	public MusicCd(String newMusicCdsTitle, int newYearOfRelease)
	{
		musicCdsTitle = newMusicCdsTitle;
		yearOfRelease = newYearOfRelease;
		
	}
	
	public String getTitle()
	{
		return musicCdsTitle;
	}
	
	public int getYearOfRelease()
	{
		return yearOfRelease;
	}
	
	
	public void setTitle(String newMusicCdsTitle)
	{
		musicCdsTitle = newMusicCdsTitle;
	}
        
	public void setYearOfRelease(int newYearOfRelease)
	{
		yearOfRelease = newYearOfRelease;
	}
	/*
	public boolean equalsName(MusicCd otherCd)
	{
		if(otherCd == null)
			return false;
		else 
			return (musicCdsTitle.equals(otherCd.musicCdsTitle));
	}
	*/
	public String toString()
	{
		return("Music Cd`s Title: " + musicCdsTitle + "\t"
			  + "Year of release: " + yearOfRelease + "\t");
	}
	
	public ArrayList<MusicCd> getMusicCd(ArrayList<MusicCd> tempList)
	{
		return new ArrayList<MusicCd>(ArrayList<MusicCd> tempList);
	}

}
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class errorCheckingOperation
{
	public int errorCheckingInteger(int checkThing, int lowerBound, int upperBound)
	{
	      int aInt = checkThing;
		
	      try
	      {
		      while((checkThing < lowerBound ) || (checkThing > upperBound) ) 
			      throw new Exception("Invaild value....Please enter the value between  " +  lowerBound + " & " +  upperBound );
	      }
	      catch (Exception e) 
	      {
		   String message = e.getMessage();
		   System.out.println(message);
	      }
	      return aInt;
       }
       
       public int errorCheckingSelectionValue(String userInstruction)
       {
	       
	       int validSelectionValue = 0;
	       try
	       {
		       int selectionValue;
		       Scanner scan = new Scanner(System.in);
		       System.out.print(userInstruction);
		       selectionValue = scan.nextInt();
		       validSelectionValue = errorCheckingInteger(selectionValue , 1, 5);
	       }
	      catch (NoSuchElementException e) 
		{
			//if no line was found
			System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
		} 
		catch (IllegalStateException e) 
		{
			// if this scanner is closed
			System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
		}
		
		return validSelectionValue;
	}
}
import java.util.*;

public class readOperation{
	
	public String readString(String userInstruction) 
	{
		String aString = null;

		try
		{
	                Scanner scan = new Scanner(System.in);
			System.out.print(userInstruction);
			aString = scan.nextLine();
		} 
		
		catch (NoSuchElementException e) 
		{
			//if no line was found
			System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
		} 
		catch (IllegalStateException e) 
		{
			// if this scanner is closed
			System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
		}
		
		return aString;
	}
	
	public char readTheFirstChar(String userInstruction) 
	{
		char aChar = ' ';
		String strSelection = null;
		try
		{
			
			//char charSelection;
	                Scanner scan = new Scanner(System.in);
			System.out.print(userInstruction);
			strSelection = scan.next();
			aChar =  strSelection.charAt(0);
		} 
		
		catch (NoSuchElementException e) 
		{
			//if no line was found
			System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
		} 
		catch (IllegalStateException e) 
		{
			// if this scanner is closed
			System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
		}
		
		return aChar;
	}
	
	public int readInt(String userInstruction) {

		int aInt = 0;

		try {
			Scanner scan = new Scanner(System.in);
			System.out.print(userInstruction);
			aInt = scan.nextInt();
		} catch (InputMismatchException e) {
			System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);
		} catch (NoSuchElementException e) {
			System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);
		} catch (IllegalStateException e) {
			System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
		}

		return aInt;

	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2007
Added on May 28 2007
4 comments
121 views