10 Replies - 1543 Views - Last Post: 29 November 2012 - 05:30 PM Rate Topic: -----

#1 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Input account info into arraylist

Posted 28 November 2012 - 06:50 AM

HI i have a bankaccounts.txt which contains the following which i outputed from an array.
:Bank Accounts currently in Account List Array:
Account Name: Morgan Checking A/c no:14213 Balance in A/c: 1000.0
Account Name: Rory Checking A/c no:13213 Balance in A/c: 1050.0
Account Name: Jake Savings A/c no:12464 Balance in A/c: 1430.0
Account Name: joe savings A/c no:2345 Balance in A/c: 2345.0

I have scanned that back in to program using code below.
File file = new File("BankAccounts.txt");
					  int ch;
					  StringBuffer strContent = new StringBuffer("");
					  FileInputStream fin = null;
					  try {
					      fin = new FileInputStream(file);
					      while ((ch = fin.read()) != -1)
					             strContent.append((char) ch);
					      fin.close();
					      
					   
					      } 
					  catch (Exception e) {
					      System.out.println(e);
					      }
					  System.out.println("\nPrinting output of Read File");
					  System.out.println( strContent.toString());

I want to add this infoback into an arraylist of type Bank_AC. All along I have been adding in accounts into arraylist by instantiating using the following
TSB1.accountList.add(new Savings ("Matt Savings", 12345, 222.22, 20,400));
public abstract class  Bank_AC //constructor for superclass
 
{
	 private String name; //Declaring variable
	   private int accNo;
	   private  double  int_rate,  overdraft; //Declaring variables for class
	   
	   protected double bal;
		
	    
		
		Bank_AC (String realname, int AccNo, Double InitialBal, double i,double Overdraft)
		{
			name =  realname;//Initializing variables to parameters above
			accNo= AccNo;
			bal = InitialBal;
			int_rate = i; 
			overdraft = Overdraft;
			
		}


What do I need to do now?

Is This A Good Question/Topic? 0
  • +

Replies To: Input account info into arraylist

#2 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Input account info into arraylist

Posted 28 November 2012 - 06:54 AM

I don't understand what your asking... what's wrong with the project? What errors are you getting? What have you tried?
Was This Post Helpful? 0
  • +
  • -

#3 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Input account info into arraylist

Posted 28 November 2012 - 08:00 AM

View Postipushmycar, on 28 November 2012 - 06:54 AM, said:

I don't understand what your asking... what's wrong with the project? What errors are you getting? What have you tried?

I want to add the items in the file into an arraylist of TYPE BANK-AC(takes 5 parameters-account name , balance etc).
I have scanned in the file.This is what is in it.
:Bank Accounts currently in Account List Array:
Account Name: Morgan Checking A/c no:14213 Balance in A/c: 1000.0
Account Name: Rory Checking A/c no:13213 Balance in A/c: 1050.0
Account Name: Jake Savings A/c no:12464 Balance in A/c: 1430.0
Account Name: joe savings A/c no:2345 Balance in A/c: 2345.0
I NEED TO KNOW HOW TO ADD THESE ITEMS(which are now in a string buffer called strContent) INTO ARRAY Called "TSB1.accountList"

Sorry last line should read:
I NEED TO KNOW HOW TO ADD THESE ITEMS(which are now in a string buffer called strContent) INTO ARRAYLIST Called "TSB1.accountList"

[quote name='mattylynch1' date='28 November 2012 - 07:59 AM' timestamp='1354114788' post='1756105']

View Postipushmycar, on 28 November 2012 - 06:54 AM, said:

I don't understand what your asking... what's wrong with the project? What errors are you getting? What have you tried?

I want to add the items in the file into an arraylist of TYPE BANK-AC(takes 5 parameters-account name , balance etc).
I have scanned in the file.This is what is in it.
:Bank Accounts currently in Account List Array:
Account Name: Morgan Checking A/c no:14213 Balance in A/c: 1000.0
Account Name: Rory Checking A/c no:13213 Balance in A/c: 1050.0
Account Name: Jake Savings A/c no:12464 Balance in A/c: 1430.0
Account Name: joe savings A/c no:2345 Balance in A/c: 2345.0
I NEED TO KNOW HOW TO ADD THESE ITEMS(which are now in a string buffer called strContent) INTO ARRAYLIST Called "TSB1.accountList"
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Input account info into arraylist

Posted 28 November 2012 - 08:09 AM

Let's break down the problem:
-Can you read the File?
-Can you create a Bank_AC object from a given line in the File?
-Can you add a Bank_AC object to the ArrayList?
Was This Post Helpful? 0
  • +
  • -

#5 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Input account info into arraylist

Posted 28 November 2012 - 08:46 AM

View Postmacosxnerd101, on 28 November 2012 - 08:09 AM, said:

Let's break down the problem:
-Can you read the File?
-Can you create a Bank_AC object from a given line in the File?
-Can you add a Bank_AC object to the ArrayList?

Yes the file is been read in to program as it appears in file in workspace directory. I have tested by printing to console...
I want to create bank ojects from the lines in the file and add to arraylist but I have no idea how to go about this.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Input account info into arraylist

Posted 28 November 2012 - 08:59 AM

Let's examine a line:

Quote

Account Name: Morgan Checking A/c no:14213 Balance in A/c: 1000.0


The first thing you want to do is split() on a colon. That will give you the following tokens:

Quote

[Account Name, Morgan Checking A/c no, 14213 Balance in A/c, 1000.0]


Think of these as elements in an array. Go through each element. Think about how you could use String methods to parse out the undesired Strings. You already know how to create an Object and add it to an ArrayList, as you demonstrate it from the code. Focus on parsing out the parameters to pass to the constructor.
Was This Post Helpful? 0
  • +
  • -

#7 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Input account info into arraylist

Posted 28 November 2012 - 09:12 AM

Yes that was the way I was thinking ineed to add "Morgan Checking A/c" to name "14213" top accno and pass these as an account into array. The problem is im not familiar with how to split it up and take out the pieces i need.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Input account info into arraylist

Posted 28 November 2012 - 09:22 AM

String.split()

Consult the API documentation for the String class to learn about this method.
Was This Post Helpful? 0
  • +
  • -

#9 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Input account info into arraylist

Posted 29 November 2012 - 01:08 AM

OK. this is the code im using to achieve the aim. I can now get the name morgan into name variable when reading from file? 2 questions:1) How do iget it to read "morgan checking" instead of just "morgan" and
2)i get "java.lang.NumberFormatException" when it reaches the follwoing line of code.
accNumber = Integer.parseInt(parser.nextToken());

How can i fix these 2 issues???

File file = new File("BankAccounts.txt");
					  int ch;
					  StringBuffer strContent = new StringBuffer("");
					  FileInputStream fin = null;
					  try {
					      fin = new FileInputStream(file);
					      while ((ch = fin.read()) != -1)
					             strContent.append((char) ch);
					      fin.close();
					      
					   
					      } 
					  catch (Exception e) {
					      System.out.println(e);
					      }
					  System.out.println("\nPrinting output of Read File");
					  System.out.println( strContent.toString());
					  //TSB2.accountDetails.add(strContent.toString());
					
					  
					  try
					    {
					  
					  BufferedReader inFileStream = 
							    new BufferedReader(new FileReader("BankAccounts.txt"));

							String name = null;
							int accNumber = 0;
							Double readBal = 0.0;

							String line = inFileStream.readLine();
							while (line != null)
							    {
								StringTokenizer parser = 
								    new StringTokenizer(line);

								// reading name, AccNumber, balance from the line
								name = parser.nextToken();
								accNumber = Integer.parseInt(parser.nextToken());
								//readBal = Double.parseDouble(parser.nextToken());
								
								System.out.println(name + ' ' + accNumber + ' ' + readBal);
								line = inFileStream.readLine();
							    }


							inFileStream.close();

						    }
						catch(FileNotFoundException e)
						    {
							System.out.println(e.getMessage());
							System.out.println("in.txt not found");
							System.exit(-1);
						    }
					  catch(IOException e)
					    {
						System.out.println(e.getMessage());
						System.out.println("Error reading in.txt");
						System.exit(-1);
						}

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Input account info into arraylist

Posted 29 November 2012 - 07:37 AM

View Postmattylynch1, on 29 November 2012 - 03:08 AM, said:

OK. this is the code im using to achieve the aim. I can now get the name morgan into name variable when reading from file? 2 questions:1) How do iget it to read "morgan checking" instead of just "morgan" and
2)i get "java.lang.NumberFormatException" when it reaches the follwoing line of code.
accNumber = Integer.parseInt(parser.nextToken());

How can i fix these 2 issues???

1) read two tokens

2) you'll get this exception if you try to use parseint on a String that doesn't represent an int. One way to avoid this is to use a try/catch: wrap the parseInt in a try block and deal with the issue as the situation warrants in the catch block. Another way to address this is to use the somewhat friendlier Scanner to replace the FileInputStream/StringTokenizer combination. Scanner has a hasNextInt() method which examines the next token and returns true if it can be parsed as an int. It also has a nextInt() method which takes the next token and returns it as an int - you can use these in combination to avoid exceptions.
However, if your input data is in a regular format, you should be able to process it as a piece. You're already reading it in as a big String. I'm going to assume your input data is line-based, one record per line. Split this into lines and you'll have an array of Strings each one representing a record(again, String.split() is the way to do this). Each record is a set of tokens -let's suppose they're name, account number, balance, each separated by one tab character, and we want those as String, int*, and float. Easy: for each line, split it on "\t" into some array. Now elements [0], [1], and [2] of the result are name, number, balance. Parse the latter two as int and float and away you go. If the number of elements is not 3, you have a bad line: deal with that error.
It should be obvious from this that I'm leading you up to writing an Account class, right?

*This is actually wrong: you don't do math with account numbers, generally, so it's better to make it a String. You might do math on the integers comprising the account number, for example to validate that it's a good account number, but you're not going to add two acount numbers and expect a meaningful result.


Looking at your code, I wonder why you read the whole file in twice?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Input account info into arraylist

Posted 29 November 2012 - 05:30 PM

Don't see why you use a StringBuffer
All the information you need is on a single line you don't need to combine/stack them
So you simply have to:
- read line until EOF
- split the line read
- create a Bank_AC out of that splitted line
- add that Bank_AC object to the ArrayList
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1