[英]Java Initializing Variable Error
我有一个错误,说我没有在buffer.write(variable);
初始化firstline,secondLine和thirdLine变量buffer.write(variable);
变量为firstLine,secondLine和thirdLine的行。 直到我在variable == JOptionPane.showInputDialog("Enter name");
周围添加while(number == null || number.equals(""))
,才会出现此错误variable == JOptionPane.showInputDialog("Enter name");
我的代码中的行。 有什么办法,同时保持我添加的代码在处理这个问题?
import java.awt.Graphics;
import javax.swing.JOptionPane;
import java.io.*;
import java.io.FileWriter;
public class CreateData {
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,
"this program writes payroll data",
"Welcome", JOptionPane.PLAIN_MESSAGE);
Write();
}
static void Write()
{
try {
String firstLine, secondLine, thirdLine, number = " ";
File check = new File("payroll.txt");
FileWriter file;
if(check.exists())
file = new FileWriter("payroll.txt", true);
else
file = new FileWriter("payroll.txt");
BufferedWriter buffer = new BufferedWriter(file);
int size, count = 1;
while(number == null || number.equals(""))
{
number = JOptionPane.showInputDialog("how many records?");
}
size = Integer.parseInt(number);
do {
while(number == null || number.equals(""))
{
firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
}
while(number == null || number.equals(""))
{
secondLine = JOptionPane.showInputDialog("Enter hours");
}
while(number == null || number.equals(""))
{
thirdLine = JOptionPane.showInputDialog("Enter wage");
}
buffer.write(firstLine);//write firstLine variable to payroll.txt file
buffer.newLine();
buffer.write(secondLine);
buffer.newLine();
buffer.write(thirdLine);
buffer.newLine();
count++;
}while(count <= size);
buffer.close();//closes the data writing stream to payroll.txt file
JOptionPane.showMessageDialog(null, "data processed",
"Result", JOptionPane.PLAIN_MESSAGE );//display message "data processed"
System.exit(1);
}
catch (IOException e) { System.out.println(e); }
}
}
这条线
String firstLine, secondLine, thirdLine, number = " ";
等于
String firstLine;
String secondLine;
String thirdLine;
String number = " ";
因此,您需要初始化firstLine,secondLine,thirdLine:
String firstLine = "";
String secondLine = "";
String thirdLine = "";
String number = " ";
添加while
围绕在您设定的变量的地方循环意味着,如果条件永远都无法满足时,变量将不会收到值。
但是那些while
循环本身是错误的。 在一般情况下, while
循环不应该有一个等待的东西,是不是里面他们改变的条件下。 由于number
是一个局部变量,没有其他线索,将改变它,它不会在循环自身内部改变:
while(number == null || number.equals(""))
{
firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
}
我敢肯定,您确实想做这个条件:
while(firstLine == null || firstLine.equals(""))
{
firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
}
因此,您应该纠正它。 尽管如此,编译器可能仍然不会满意了,所以你应该,事实上,提供一个默认值,当你声明变量,并作为其他答案告诉你,以下声明:
String firstLine, secondLine, thirdLine, number = " ";
不要将所有变量都设置为" "
-仅将number
变量设置为。 您需要单独分配给每个人。
您设置不应该是值" "
(空格)。 因为这不符合任何一个条件(这不是零和它不是空的),所以它不会去的环内,你会想知道为什么你刚刚的空间。 您应该将其设置为null
或空字符串。
我来回答类似的问题你的问题:
int x;
while(false) {
x = 10;
}
System.out.println(x);
因为有你进入while循环没有保障 ,因此存在不能保证 x
被初始化。
在你的代码出现同样的问题。 即使在逻辑上确信您将进入while loop
,编译器也必须确定。
因此,请初始化变量。 这不会对它们进行初始化你认为它的方式。 它,事实上,只有初始化最后一个变量。
String firstLine, secondLine, thirdLine, number = " ";
尝试这个:
String firstLine, secondLine, thirdLine, number;
number = " ";
firstLine = null;
secondLine = null;
thirdLine = null;
您也可以这样做:
String firstLine, secondLine, thirdLine, number;
number = " ";
firstLine = secondLine = thirdLine = null;
请注意,您不必将变量初始化为null
:也可以将它们初始化为任何其他String
。
只是初始化三个变量为空字符串.. = firstLine中无效,二线= NULL,thirdLine = NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.