I have a file which reads
Office 1
3
39 7
39 7
39 6.5
Office 2
2
19 8
19 8
This shows which office it is, the next number down is the number of managers in that office then the number of hours each works followed by what they earn per week
I am trying to create a loop so my java file can read the hours then multiply them by the hourly rate then save it as a double then move onto the next office and do the same so I can use the doubles later on in my program, then the loop will end when it gets to the final office I know how to read from the file I am just struggling to get a loop which can do what I specified above
Any help would be much appreciated thanks so much!
EDIT......
public static void main(String[] args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("ExternalData.txt"));
String name;
double employees;
double OfficeRate;
double OfficeHours;
double OfficeRate2;
double OfficeHours2;
double OfficeRate3;
double OfficeHours3;
double OfficeRate4;
double OfficeHours4;
name = inFile.nextLine ();
employees = inFile.nextDouble();
OfficeRate = inFile.nextDouble();
OfficeHours = inFile.nextDouble();
OfficeRate2 = inFile.nextDouble();
OfficeHours2 = inFile.nextDouble();
OfficeRate3 = inFile.nextDouble();
OfficeHours3 = inFile.nextDouble();
OfficeRate4 = inFile.nextDouble();
OfficeHours4 = inFile.nextDouble();
double Employee1 = OfficeRate * OfficeHours;
double Employee2 = OfficeRate2 * OfficeHours2;
double Employee3 = OfficeRate3 * OfficeHours3;
double Employee4 = OfficeRate4 * OfficeHours4;
double weeklypay = Employee1 + Employee2 + Employee3 + Employee4;
Now this works dont get me wrong but it's well to long than it should be surely?
Sorry for late reply and I have ended up writing whole code for you, so understand how this is accomplished.
Anyhow the way you are currently doing is not efficient in case file contains hundreds of office entries.
Just make sure you have intact file content as you have shown in your Post. If something is missing then this following code could get crash or give you unexpected results.
Here is code for you to accomplish your following requirements. In this case you won't be able to save any data from file as you can't use
Arrays
. May be if you have luxury to useArrayList
then make use of that to store file data.If you have any questions let me know. Good Luck.