[英]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.