We are about to switch to a new forum software. Until then we have removed the registration on this forum.
so i am trying to make a payroll program, right now i am getting caught up on getting the items of the array to print properly, i have my data files in my folder and they are being pulled properly but i cant get them to print on to the sketch, im not sure if im not using something properly or if im not declaring my variables properly but im getting caught up and could use a hand
in my data file for EmployeeID.txt it has 1,2,3,4 in it exactly like that
in my data file for EmployeeName.txt it has Alabaster Snowball,Bush Evergren, Pepper Minstix, Sugerplum Mary exactly like that
here is what i have so far
//Carmine
//
//Lab 5 Payroll Lab
//
PFont font;
String[] EmployeeID;
String[] EmployeeName;
final float MINIMUM_WAGE=9.84;
final float EMPLOYEE_1_HOURS=40;
final float EMPLOYEE_2_HOURS=25.5;
final float EMPLOYEE_3_HOURS=45;
final float EMPLOYEE_4_HOURS=42;
void setup() {
size(800, 600);
background(255);
EmployeeID=loadStrings("EmployeeID.txt");
EmployeeName=loadStrings("EmployeeName.txt");
font= loadFont("ComicSansMS-48.vlw");
textFont(font, 32);
fill(0);
text("Sam's Payroll", 250, 50);
}
void draw() {
//print text from arrays onto sketch
textSize(16);
textAlign(LEFT);
println(EmployeeID,50,50);
}
ive tried using printArray, print, println, ive tried changing stuff around but it doesnt seem to work and i have been looking on the reference for processing but cant find something to help yet so maybe someone can here.. Thanks again im probably making a really stupid error but it happens im still learning
Answers
Helpful example code follows:
Study this and try applying what it demonstrates to your own code. Notice that, since I don't have your data files, I am spoofing the data with the included
fakeStrings()
function.Look at
text()
in the referencehttps://www.processing.org/reference/
This is Homework
Please post the content of your data file and format it as code too
In my opinion you have a data file with multiple columns (3) - or your classmates have - so I am unsure why you work with 2 files
Anyway, loadStrings() (see reference) gives you an array so in draw() you want to for loop i over
employeeID
!!!And use
text(EmployeeID[i], 44, i*44);
inside for loopRead more of the reference in future
https://www.processing.org/reference/for.html
https://forum.processing.org/two/discussion/27892/calculate-pay#latest
@TFGuy44 i appreciate all your help but your example has me so confused mainly because i failed to include my data files properly most likely
@Chrisir i am going to add them in this comment here and also what i currently have
ok so i have been messing around and i got what i want to work but now have some further questions
i am going to show what i currently have then ask my questions
here are my data files
i used some examples from class and it got me to this point, my strings load in and show what is in them but now i have questions
2 . i am trying to use the text() command to display my data from String[] IDs and String[]Names but when i was trying i am getting an error saying that i cannot use string[] with it, i thought it would work since it shows on the reference that strign can be printed that way but i guess since its an array (i think) it cannot? since i do need to print my array items onto the actual sketch how could i go along to do this?
Your 2 data files and additional data
as I said, instead of having 2 data files PLUS employee data inside the sketch (very bad programming style):
you should have ONE file like a table:
this could be loaded as a table using the command loadTable and then Table etc. - see reference.
Your questions
1.
String[] of course is an array. It's of type String. int[] is an array of type int. This means the type of the variable in the slots of the array is String / int.
I already said that in my previous posts!!! Maybe read more slowly.
see also:
https://www.processing.org/reference/loadStrings_.html
quote:
2.
text(IDs, 10, 10, 80, 80); won't work on an array (IDs). text can't display an entire array. Instead you need to display each slot / String separately.
Instead, for loop over IDs as I explained already above
Chrisir ;-)
@chrissir using that makes sense but this assignment requires us to "1. Read 4 numbers from a text file into an array 2. Read 4 names from a text file into an array 3. Display all items of the arrays in the original order" thats why i need to use arrays,
if i recall correctly it was said in class an array can be multidimensional (probably not the correct term) but like in the array i would be able to store the employee ids, name, and hours all in one array if i do recall this correctly, i havent been able to find this anywhere yet looking more before i post my comment i found what i am looking for.. so could i use a 3-D array to store all my information? i was looking and actually found another post where you helped someone with one and it looks like i could also use it to work for me?
when you want to work with 2 files do me favour and make this :
a 3rd file.
to be honest with you, better just work with the 3 arrays and make it work.
Let's do the 2d array later (3D array is something else entirely).
Here comes a small explanation about 2D arrays anyway:
When you have String[] IDs and String[] Names and float[] hoursEmployee these are 1D arrays (like a shopping list, one dimension) and they are parallel in that the data of one employee are distributed over the 3 arrays. So for one employee look into the same line number of all 3 shopping lists.
Now, when you place the 3 shopping lists next to each other you'll see a grid like a game of chess board. each row is an employee, each column is one property (ID, name, hours). That there are 3 columns has nothing to do with 2D dimension. It's still a 2D array with 1000 columns. (The dimension thing means: 1D=shopping list, 2D=grid, 3D=cube, 4D=list of cubes...)
So you don't need a 3D array but a 2D array.
see here for 1D array :
https://www.processing.org/tutorials/arrays/
and for 2D
https://www.processing.org/tutorials/2darray/
@chrissir probably is a good idea to work with easier stuff till i really understand what i am doing,
now im trying to use that text statement as you show and im getting an ArrayOutOfBoundsException:1 error and im not sure since to me it looks like the for loop,
two i dont follow what you mean by when you saying when im working with two files do me a favour and make this with my float variables... ohh i think i get it now; your saying make the hours into another file and make them an array as well and just work with the three arrays as you also say.. makes sense
ok so heres what i currently have after everything youve helped with so far, i believe this is the route you were saying would be best
i started the void calculatePay because i will be using a function to calculate the pay for all the items in the array
as of now all items in the array do print out in the console, the hour array prints twice for some reason tho.. also i get the ArrayOutOfBoundsException:1 still so i cant seem to get the text statement to print on the sketch to work yet... (
i appreciate you taking so much time to really explain all this stuff to me because now it makes a lot more sense @chrissir
Yes.
show your entire code; how can we find the error without seeing the code?!
@chrissir quite convenient that i posted it up right as you asked
I believe you have this data in the new file now?
I want you to delete these lines then:
Access too the hard drive is time expensive; hence move these lines from draw into setup
You have to delete String[] in all three lines!!!
This: text(IDs[0],10,10,80,80); better IDs[i] !!!!!!
Your for loops are not ok. Since all arrays are parallel they have the same length.
this is very wrong:
first you need to pass the hours to it as a parameter
Second you need to return the result
so you can call calculatePay in draw like
text (calculatePay(Hours[i]), 300, i*60);
Gotta go
see you tomorrow
hit F5 here to see discussion
Bye
ok i do have one more question so dropping the string[] gives a error that the variables do not exist?
Thank you for all the help today!
You need to create your variables for them to exist, and you need to specify what their types are when you create them! If you remove
String[]
from the declaration of your variables, how is your sketch going to know what type of variable you want?For example, this defines a variable that is a String:
If you leave off the
String
part of that line:It no longer looks like a declaration - instead it becomes an assignment! And it's an assignment to a variable that doesn't exist, because it hasn't been declared!
The same is true with arrays.
This has a declaration - that there is an array of strings - and then an assignment - it assigns values to those strings.
If you leave the type out of the declaration, it's NOT a declaration!
I am sorry.
In one of your previous versions you had
Before setup
Hence it was declared globally
Now you mustn’t repeat the String [] part because then you would declare a variable locally
@tfguy44 thank you for clearing that up @chrissir no issues
now im still confused to why i cannot get thre text statement you were saying to use to work.. for any statement that tries to print the array it gets a ArrayOutOfBoundsException:1 error on every statement, the array's data still prints to the console though which confuses me.
i will attach what i currently have, maybe someone else can see what im missing; why it wont print to the sketch and giving me errors still
Line 23 < instead of <=
You can improve the text command: multiply the y value with i so that you have a new line per employee
X value is the column so that value you can choose freely
Also display Hours and the resulting payment
After line 16 try printArray(IDs);
Does the loading work? Or do you get any error message?
No, that won’t work. Instead replace comma with return
The numbers must be under each other, one number per line
Same for names and hours!!!
loadStrings works in a way that it puts each line of the file in one slot of the array IDs etc.
@chrissir you are the best. really thank you for all the help with this!
;-)
i do have another question now, it displays the array from the second position of the array not the first
Add 60 to the y value for the text()
Make sure first line of the files aren’t empty ;-)
60 is an example number and represents the distance from the upper screen border
@chrissir i try changing the values and it still skips right to the second value all files are formatted to the top with no line above em
Does it skip the first also with println - or only with text() ?
Please post your entire code again and the content of data files
Also try text without the last 2 parameters
After line 16 try printArray(IDs);
What does that give you?
@chrissir without the last two parameters it still prints the array skipping the first one, when i use the printArray statement it shows all items that are in my data file
here is my current code
here is my EmployeeHours.txt 40 25.5 45 42 here is my EmployeeID.txt 1 2 3 4 and here is my EmployeeName.txt Alabaster Snowball Bush Evergren Pepper Minstix Sugerplum Mary
This
text(IDs[i], 100, 150*i);
Better
text(IDs[i], 100, 150*i + 60);
@chrissir perfect! thank you now i just need to work on a function and make it look better and i am done!
Yes! Well done.
@chrissir sooo im working on this function and im aware that i cant use > to compare with a string but i cant seem to find something equivalent, also for my function it wants floats not strings, maybe im missing something else.. hopefully my function is actually some what correct, it makes sense to me and comparing to previous functions ive made it looks pretty accurate
so here it all is again lol
text (calculatePay(Hours[i]), 300, i*60));
Better
text(calculatePay(float (Hours......
instead of void calculatePay you want float calculatePay
Last line in function = return result;
Hours over 40 receive time-and-a-half pay. All employees are paid minimum wage of Alaska 9.84)
I think when it’s >40 you want only the amount of hours over 40 to be multiplied with a higher factor and not all hours
so i think im getting further still have errors tho on calculatePay in the draw and processing doesnt like my operators in my function
This after line 62
return result;
Line 57 : define rate first!
Don’t use Hours[i] in the function, use the parameter hours!!!!!!!
Line 37 one missing )
Post your entire code
@chrissir alright its a lot better now but im not seeing where the missing ) needs to be in line 37?
Does the code run?
) after]
Not at end of line...
ok i figured something out on my own finally it works just like it should thank you so much @chrissir you should work at my school
Congratulations