\"I am trying to pass the arrays from the RacesDemo to Races class constructor.
ID: 3681497 • Letter: #
Question
"I am trying to pass the arrays from the RacesDemo to Races class constructor. i got the code here. just a little help"
import java.io.*; // import filereader to read files import java.util.StringTokenizer;
public class RacesDemo { public static void main(String[] args)throws IOException {
int participants = 5; // store number of participants
import java.io.*; // import filereader to read files
import java.util.StringTokenizer;
/**
* Write a description of class realdemo here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RacesDemo
{
public static void main(String[] args)throws IOException
{
int participants = 5; // store number of participants
String[] customerList = new String[participants]; // array to hold data from txt file
int[] accnum= new int[participants]; // array to hold maccount numbers
int [] pin= new int[participants]; // array to hold pin
double[] initbal = new double[participants]; // array to hold tinitial balance
FileReader freader = new FileReader("Readme.txt"); // Class to read data in text file
BufferedReader inputFile = new BufferedReader(freader); // Class to read each line
//R//ace4Cure rockRace = new Race4Cure();
// Read each line of data from file
for (int index = 0; index < participants; index++)
{
customerList[index] = inputFile.readLine();
}
inputFile.close(); // Closes the txt file
// Display file contents
for (int index = 0; index < customerList.length; index++)
{
// System.out.println(customerList[index]);
}
for (int index = 0; index < customerList.length; index++)
{
// StringTokenizer class to separate each line into separate variables
StringTokenizer strTokenizer = new StringTokenizer(customerList[index], ",");
accnum[index] = Integer.parseInt(strTokenizer.nextToken());
pin[index] = Integer.parseInt(strTokenizer.nextToken());
initbal[index] = Double.parseDouble(strTokenizer.nextToken());
// save total pledge amount by each person to an array
}
//IM TRYING TO PASS THIS TO THE Races Constructor.
Races my=new Races(int[] accnum,int[] pin,double[] initbal);
}
}
//below is the other class calles Races
public class Races {
public Races(int[] a,int[] p,int[] i)
for(int i=0;i<5;i++{
System.out.println(accnum[i]+pin[i]+initbal[i]); } }
Explanation / Answer
// The path name for the input file has to be specified as follows:
FileReader freader = new FileReader("E:/Files/Readme.txt"); // Class to read data in text file
// the initial balance is of data type double not int
public Races(int[] accnum, int[] pin, double [] initbal) { // constructor function with parameters
// and also while calling just pass the variable names as parameter - not their data types
Races mymy;
mymy = new Races(accnum, pin, initbal);
// the entire changed code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package racesdemo;
import java.io.*; // import filereader to read files
import java.util.StringTokenizer;
/**
* A description of class race demo:
*
* @author Admin
* @version 1.0
*/
/**
*
* @author Admin
*/
//below is the other class called Races
class Races {
public Races(int[] accnum, int[] pin, double [] initbal) { // constructor function with parameters
for(int j =0; j<5; j++)
System.out.println( accnum[j] + pin[j] + initbal[j] );
} // end for loop
} // end function
} // end race class
public class RacesDemo {
static int[] accnum, pin;
static double[] initbal;
public static void main(String[] args)throws IOException {
int participants = 5; // store number of participants
String[] customerList = new String[participants]; // a string array to hold data from txt file
int[] accnum= new int[participants]; // array to hold m account numbers
int [] pin= new int[participants]; // array to hold pin
double[] initbal = new double[participants]; // array to hold tinitial balance
FileReader freader = new FileReader("E:/Files/Readme.txt"); // Class to read data in text file
BufferedReader inputFile = new BufferedReader(freader); // Class to read each line
// Race4Cure rockRace = new Race4Cure();
// Read each line of data from file
for (int index = 0; index < participants; index++)
{
customerList[index] = inputFile.readLine();
}
inputFile.close(); // Closes the txt file
// Display file contents
for (int index = 0; index < customerList.length; index++)
{
// System.out.println(customerList[index]);
}
for (int index = 0; index < customerList.length; index++)
{
// StringTokenizer class to separate each line into separate variables
StringTokenizer strTokenizer = new StringTokenizer(customerList[index], ",");
accnum[index] = Integer.parseInt(strTokenizer.nextToken());
pin[index] = Integer.parseInt(strTokenizer.nextToken());
initbal[index] = Double.parseDouble(strTokenizer.nextToken());
// save total pledge amount by each person to an array
} // end for
Races mymy;
mymy = new Races(accnum, pin, initbal);
} // end main
} // end public class race demo
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.