I am trying to read in a file and separate the file into multiple arrays that ca
ID: 3848544 • Letter: I
Question
I am trying to read in a file and separate the file into multiple arrays that can be searched through and manipulated.
The file is in this format "lastName, firstName" position department "salary""lastName, firstName" position department "salary"....
example:
"AARON, ELVIA J" WATER RATE TAKER WATER MGMNT "$87,228.00"
"AARON, JEFFERY M" POLICE OFFICER POLICE "$75,372.00"
"AARON, KARINA" POLICE OFFICER POLICE "$75,372.00"
The file has copius amounts of entries.
Here is my code so far, I think the problem is in the strLine.split() method, but I am not sure.
package programassignment3;
import java.io.*;
import java.util.*;
public class ProgramAssignment3 {
String name;
String position;
double salary;
public ProgramAssignment3(String name, String position, double salary){
this.name = name;
this.position = position;
this.salary = salary;
}
public static void main(String[] args) {
ArrayList<ProgramAssignment3> employee = new ArrayList<>();
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("currentEmployee.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
// split the data by
String[] tokenize = strLine.split("");
String tempName = tokenize[0];
String tempPosition = tokenize[1];
int tempSalary = Integer.parseInt(tokenize[3]);
ProgramAssignment3 tempObject = new ProgramAssignment3(tempName, tempPosition, tempSalary);
employee.add(tempObject);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
for (ProgramAssignment3 each : employee) {
System.out.println("==========================");
System.out.println(each);
System.out.println();
}
}
}
Thanks!!
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
package programassignment3;
import java.io.*;
import java.util.*;
public class ProgramAssignment3 {
String name;
String position;
// double salary;//salary can't be converted to double..you have to write another method for that with proper logic(because there coma and $ in the salary)
//hence declare it also as string
String salary;
public ProgramAssignment3(String name, String position, String salary){
this.name = name;
this.position = position;
this.salary = salary;
}
public static void main(String[] args) {
ArrayList<ProgramAssignment3> employee = new ArrayList<>();
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("currentEmployee.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
// split the data by
String[] tokenize = strLine.split(""");//you have to split at quotes..
//tokenize[0] will be empty
String tempName = tokenize[1];
String tempPosition = tokenize[2];
//added line
String tempSalary = tokenize[3];
//System.out.println(tokenize.length);
// System.out.println(tokenize[0]+" 1 "+tokenize[1]+" 2 "+tokenize[2]+" 3 "+tokenize[3]);
//int tempSalary = Integer.parseInt(tokenize[3]);
ProgramAssignment3 tempObject = new ProgramAssignment3(tempName, tempPosition, tempSalary);
employee.add(tempObject);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
for (ProgramAssignment3 each : employee) {
System.out.println("==========================");
System.out.println(each);
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.