Computer Science Canada Inputting for a Loop. |
Author: | GDoughtUpInIt [ Sun Jul 18, 2004 9:40 pm ] |
Post subject: | Inputting for a Loop. |
Hey, my program is designed to help someone organize their company departments. I want the user to enter how many departments he/she has, and then the program uses the user's input to determine how many times the company form comes up. Any help would be appreciated. Here is my code. Maybe someone can help me out with it. Quote: import java.io.*;
import java.text.*; class Departments { public static void main (String[] args) throws IOException { int x=0; String departments; BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many departments would you like to enter?"); departments = in.readLine (); while (x=departments){ String department; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Department Name:"); department = in.readLine (); String employees; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Employees:"); employees = in.readLine (); String costPerEmployee; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Cost Per Employee:"); costPerEmployee = in.readLine (); String sales; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Sales:"); sales = in.readLine (); x=x+1; } } } |
Author: | Tony [ Sun Jul 18, 2004 10:10 pm ] | ||
Post subject: | |||
you'd use a for loop
|
Author: | GDoughtUpInIt [ Mon Jul 19, 2004 9:47 pm ] | ||
Post subject: | |||
Okay, I changed it to a for loop but now the interpreter says, "operator < cannot be applied to int,java.lang.String for (int x=0; x<departments; x++){" Here is my current, non-working code:
|
Author: | wtd [ Tue Jul 20, 2004 11:57 am ] | ||
Post subject: | |||
Not tested, but it should set you on the right path.
|
Author: | GDoughtUpInIt [ Tue Jul 20, 2004 1:54 pm ] |
Post subject: | |
Well the code works, I just had to change the variable "prompt" to "prompts" on line 17. Now I need to do something like sales-(employees*cost per employee) to see if each departments is profitable. I'm not sure how to go about this. Any suggestions? |
Author: | Tony [ Tue Jul 20, 2004 2:04 pm ] |
Post subject: | |
well first of all you'd have to declear variables as global to the class, not local to the main(). This way other methods can access the same variables. then create a boolean function in the department class called isProfitable which would return true if the restult of your calculation (revenue - cost) is possitive or false otherwise. |
Author: | wtd [ Tue Jul 20, 2004 2:25 pm ] | ||||
Post subject: | |||||
Sorry about the typo. I was rewriting that code in O'Caml, just to test my functional programming skills and it got lost in the mess of code. ![]() But I digress... You have all of the input about the departments in strings. For number of employees, cost per employee and sales, you'll need to convert to int. You can use Integer.parseInt to do this.
If anyone's interested, the O'Caml equivalent so far looks like:
Hmm... I might code up an object-oriented version in both languages, just for fun. |
Author: | GDoughtUpInIt [ Thu Jul 29, 2004 1:10 pm ] |
Post subject: | |
Hey guys. Need a little more guidance. Quote: import java.lang.*;
import java.io.*; public class Departments2 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.print("How many departments would you like to enter? "); String[][] departments = new String[Integer.parseInt(in.readLine())][]; String[] prompts = { "Department Name", "Employees", "Cost per Employee", "Sales" }; for (int i = 0; i < departments.length; i++) { departments[i] = new String[prompts.length]; for (int j = 0; j < prompts.length; j++) { System.out.println(prompts[j] + ":"); departments[i][j] = in.readLine(); } } double[] profits = new double[departments.length]; for (int k = 0; k < departments.length; k++) { double num_employees = Integer.parseInt(departments[k][1]); double cost_per_employee = Integer.parseInt(departments[k][2]); double sales = Integer.parseInt(departments[k][3]); profits[k] = sales - (num_employees * cost_per_employee); } boolean[] profitable = new boolean[departments.length]; for (int m = 0; m < profitable.length; m++) { profitable[m] = profits[m] > 0; } } } That code asks the user how many departments and allows the user to enter the information. Now I need to have this output at the bottom... "Department A Profits: $00.00 Department B Profits: $00.00 Department C Profits: $00.00 Department D: Profits: $00.00 Together, the departments are/are not profitable." If I could get a code that could do this, that would be great. I'd like to see the changes made by an expert so that I can compare and learn how it's done. I think it would be a good lesson for a beginner like myself. Any help would be greatly appreciated. Thanks guys. |
Author: | wtd [ Thu Jul 29, 2004 6:11 pm ] | ||
Post subject: | |||
Hopefully you can learn from this.
|
Author: | GDoughtUpInIt [ Thu Jul 29, 2004 11:31 pm ] |
Post subject: | |
Hey. I examined the code, and have learned a lot about how to do this kind of looping. Thank you for your help. |
Author: | wtd [ Fri Jul 30, 2004 7:00 pm ] | ||
Post subject: | |||
And here's an object-oriented version of it, just to further enlighten. ![]()
|