1 Replies - 6227 Views - Last Post: 22 October 2009 - 04:53 AM Rate Topic: -----

#1 Korea  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 09

How can I store the data into ArrayList?

Post icon  Posted 22 October 2009 - 04:32 AM

Hello

I have encountered a problem in JAVA program.
I would like to request users to input something,
then the inputed data will be stored into the ArrayList.

For example,
acc_no 101 originally has two ArrayList<Trip>,
ie,
list.add( new Trip( 20000));
list.add( new Trip( 15000));
B_C b = new B_C( 101, list );

if a user inputs 10000 for acc_no 101,
then,
acc_no 101 will have 3 ArrayList<Trip>

However, I do not know how to add the data into ArrayList.
ie. how to store data like list.add( new Trip( 10000));

Besides, there is another problem.
In the ArrayList<members>
there are 2 members, 101 & 102

but when I input acc_no as 102
the program cannot verify 102's existence
I am afraid that there is logical problem in my for each statement.

So, would anyone help me to solve this problem?
Thanks!

here is my original code:
import java.util.*;
import java.io.*;

class B_C
{
	protected int acct_no;
	protected ArrayList <Trip> list;
	
	public int Getacc()
	{
		return this.acct_no;
	}

	public B_C( int acct_no, ArrayList <Trip> mileage )
	{
		this.acct_no = acct_no;
		list = mileage;
	}

	public String toString()
	{
		return Getacc() + "\n" + list;
	} 
}


class Trip
{
	private int no_of_km;

		public int get_km()
		{
				return this.no_of_km;
		}
				
		public Trip( int no_of_km )
		{
		this.no_of_km = no_of_km;
		}

		public String toString()   
		{
				return "no_of_km : " + get_km();
		}

} // end of class Trip

public class MM
{
  public static void main( String[] args ) throws Exception
  {
	  ArrayList <Trip> list = new ArrayList <Trip>();

	  list.add( new Trip( 20000));
	  list.add( new Trip( 15000));
	  B_C b = new B_C( 101, list );
	  B_C d = new B_C( 102, list );

		ArrayList <B_C> members = new ArrayList<B_C> ();
		members.add(b);
	members.add(d);

	BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
	System.out.printf(" Please enter acct_no: ");
	int acc_no = Integer.parseInt( in.readLine() );

	for ( B_C bc : members )
	{
		if ( bc.Getacc() == acc_no )
		{
			System.out.printf(" INPUT mileage earned: ");
			int mileage = Integer.parseInt( in.readLine() );

			// how can I store the data into ArrayList <Trip> 			

			return;
	}

		else
		{
			System.out.printf("Wrong NO\n");
			return;
		}
	}
  } // end of main
}




Is This A Good Question/Topic? 0
  • +

Replies To: How can I store the data into ArrayList?

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How can I store the data into ArrayList?

Posted 22 October 2009 - 04:53 AM

Hi
add a method to the B_C class to add a trip addTrip(int numberOfKm)
public void addTrip(int numberOfKm){
this.list.add(new Trip(numberOfKm));
}


just get a number of km from the user, and add it to the list of the appropriate B_C.
use it as:
scan the number of B_C
scan number of km
add the new trip using the addTrip method to the certain B_C

hope it helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1