Question

I'm trying to store a bunch of variables through bufferedreader. I'm brand new to Java and frankly don't really understand completely what it even does, but I know that it accepts input from the user and am trying to get it to store a bunch of variables for me. Here is the code:

// import required packages for the program, importing all classes from packages to be on the safe side
import java.io.*;
import java.util.*;
import java.text.*;

public class Example
{
// need to use throws IO Exception because we are dealing with input and output, simple main method will not suffice
public static void main(String[] args) throws IOException
{
    // declare department names as strings
    String department1;
    String department2;
    String department3;
    String department4;

    // declare number of employees in each department as ints
    int employees1;
    int employees2;
    int employees3;
    int employees4;

    // declare cost per employee in each department as doubles
    double cpe1;
    double cpe2;
    double cpe3;
    double cpe4;

    //declare sales in each department as doubles
    double sales1;
    double sales2;
    double sales3;
    double sales4;

    // declare in as a BufferedReader- used to gain input from the user
    BufferedReader in;
    in = new BufferedReader(new InputStreamReader(System.in));

    //get input from the user to define department names
    System.out.println("What is the name of department 1?");
    department1 = in.readLine();
    System.out.println("What is the name of department 2?");
    department2 = in.readLine();
    System.out.println("What is the name of department 3?");
    department3 = in.readLine();
    System.out.println("What is the name of department 4?");
    department4 = in.readLine();
    System.out.println("\n");

    //get input fromm the user to define number of employees
    System.out.println("How many employees are in department 1?");
    employees1 = in.readLine();
    System.out.println("How many employees are in department 2?");
    employees2 = in.readLine();
    System.out.println("How many employees are in department 3?");
    employees3 = in.readLine();
    System.out.println("How many employees are in department 4?");
    employees4 = in.readLine();
    System.out.printlin("\n");

    // get input from user to define cost per employee in each department
    System.out.println("What is the cost per employee in department 1?");
    cpe1 = in.readLine();
    System.out.println("What is the cost per employee in department 2?");
    cpe2 = in.readLine();
    System.out.println("What is the cost per employee in department 3?");
    cpe3 = in.readLine();
    System.out.println("What is the cost per employee in department 4?");
    cpe4 = in.readLine();

    // get input from user to define sales in each department
    System.out.println("What are the sales in department 1?");
    sales1 = in.readLine();
    System.out.println("What are the sales in department 2?");
    sales2 = in.readLine();
    System.out.println("What are the sales in department 3?");
    sales3 = in.readLine();
    System.out.println("What are the sales in department 4?");
    sales4 = in.readLine();

I keep getting an error saying that incompatible types are occurring when I try and run the program. Again, very new to this so a any simple explanations would be very much appreciated. Thank you!

Was it helpful?

Solution

The readLine method returns String. This code will not even compile. If you want to get an int value, you need to call for example Integer.parseInt(ln), where ln is the line you read from the input. Analogically, if you want to get a double value, you need to call for example Double.parseDouble(ln), where ln is the line you read from the input.

OTHER TIPS

The issue is one of type; you have several different data-types present, but you only read Strings. I would use a java.util.Scanner like so -

java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("What is the name of department 1?");
if (in.hasNextLine()) {
  department1 = in.nextLine();
}
// ...
System.out.println("How many employees are in department 1?");
if (in.hasNextInt()) {
    employees1 = in.nextInt();
}
// ...
System.out.println("What is the cost per employee in department 1?");
if (in.hasNextDouble()) {
  cpe1 = in.nextDouble();
}

Take a look at the Scanner Class This should do a lot of what you need. It allows you to read in inputs as Integers/Strings/Doubles, without having to do any manual conversion

in.readLine() yields a String - not automatically convertible to int or double. Consider Integer.parseInt(string).

The readLine() method returns a String. You're trying to store that String in int and double variables. There are methods in the wrapper classes (like Integer and Double) for converting from a String to a number.

You might also consider using the Scanner class instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top