473,386 Members | 1,820 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,386 software developers and data experts.

How Do I Pass Certain Parts of an Array to Create Parts of an Object

I need to create multiple "runner" objects each with three variables. The variables should be name, runner id, and amount raised. I have to read in two text files to get the information for these. One text files contains the runner id and runner name. The other text file contains the runner id and donation amount. I created a "driver" class to read in the files and pass the info to a constructor in a "runner" class to create the runner object. I used a string tokenizer to read in the individual parts from the files. I just don't know how to take those individual parts of the arrays and pass them to the correct methods to set the variables for the object. This is the code I have for the driver class. The lines with System.out.println("") are just so I could see the contents of the arrays.

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6.  
  7.  
  8. public class Driver {
  9.     public static void main(String[] args) throws FileNotFoundException{
  10.         ArrayList<String> data = new ArrayList<String>();
  11.         ArrayList<String> donations = new ArrayList<String>();
  12.  
  13.         Scanner scanner1 = new Scanner(new File("data.txt"));
  14.         while (scanner1.hasNextLine()) {
  15.             String str1 = scanner1.nextLine().trim();
  16.             String[] tokens1 = str1.split(",");
  17.             for (String s : tokens1){
  18.                 data.add(s);    
  19.             }
  20.         }    
  21.         System.out.println(data);
  22.         scanner1.close();    
  23.  
  24.  
  25.  
  26.  
  27.         Scanner scanner2 = new Scanner(new File("donations.txt"));
  28.         while (scanner2.hasNextLine()){
  29.             String str2 = scanner2.nextLine().trim();
  30.             String[] tokens2 = str2.split(",");
  31.             for (String t : tokens2){
  32.                 donations.add(t);
  33.             }
  34.         }
  35.         System.out.println(donations);
  36.         scanner2.close();
  37.     }
  38. }
Apr 28 '15 #1
0 1274

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

Similar topics

1
by: Robert Oschler | last post by:
With PHP 4, is there a way to convert an associative array to an object? For example, suppose I have the following arrays inside a nested associative array ($nestedAA): $AA1 = 'fieldValue1'; ...
4
by: Sunny | last post by:
Hi, I am at present working on a jsp page which has a single text field. Upon entering a value in the field, i am trigerring an onChangeEvent() which calls a method of javascript. I am also...
2
by: HairlipDog58 | last post by:
I have a component that implements a property called obj of type object. The intent is that a user can pass in any value or array of values and I will be able to retrieve the values. If the...
2
by: davidw | last post by:
As I asked in last post, I want to put some logic in a object and all my webcontrol instance will access that object, the object is responsed to retrieve data from database if the data has not been...
3
by: AAOMTim | last post by:
Give the method ProcessArray as follows: void ProcessArray(Object objects) { foreach (Object object in objects) { // Do something } }
8
by: bill | last post by:
Turning on error_reporting(E_ALL); was quite an eye opener as to how much "fixing" PHP will do for the sloppy coder. I fixed all of the errors except: Notice: Undefined property: parts in...
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
29
by: Why Tea | last post by:
Suppose you have a 2-dimensional array (matrix) in main() and you want to pass it to a function to do some processing, you usually pass it as a pointer to the first element. But, from the function,...
3
by: tshad | last post by:
I am calling a function that requires an Exception which you normally get from a Catch ( Catch(Exception exc)). But I have a message I am trying to pass to this funtion, but since it requires an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.