我正在尝试通过bufferedreader存储一堆变量。我是Java的新手,并且坦率地说它并不完全理解它甚至做了什么,但我知道它接受来自用户的输入并且我试图让它为我存储一堆变量。这是代码:
// 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();
我一直收到一条错误消息,说我尝试运行程序时出现了不兼容的类型。再次,这是非常新的,所以任何简单的解释将非常感激。谢谢!
答案 0 :(得分:3)
readLine
方法返回String
。
这段代码甚至不会编译。
如果您想获得int
值,则需要调用示例Integer.parseInt(ln)
,
其中ln
是您从输入中读取的行。类似地,如果您想获得double
值,则需要调用示例Double.parseDouble(ln)
,
其中ln
是您从输入中读取的行。
答案 1 :(得分:2)
问题是类型之一;你有几种不同的数据类型,但你只阅读字符串。我会像java.util.Scanner
这样使用 -
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();
}
答案 2 :(得分:1)
看看Scanner Class这应该可以满足您的需求。它允许您以整数/字符串/双精度读入输入,而无需进行任何手动转换
答案 3 :(得分:0)
in.readLine()
产生一个String - 不能自动转换为int或double。考虑Integer.parseInt(string)
。
答案 4 :(得分:0)
readLine()方法返回一个String。您正试图将该String存储在int和double变量中。包装类中有一些方法(如Integer和Double),用于从String转换为数字。
您也可以考虑使用Scanner类。