简体   繁体   English

Java初始化变量错误

[英]Java Initializing Variable Error

I have an error that says I haven't initialized my firstline, secondLine, and thirdLine variables in the buffer.write(variable); 我有一个错误,说我没有在buffer.write(variable);初始化firstline,secondLine和thirdLine变量buffer.write(variable); lines with variable being firstLine, secondLine & thirdLine. 变量为firstLine,secondLine和thirdLine的行。 This error didn't appear until I added while(number == null || number.equals("")) around the variable == JOptionPane.showInputDialog("Enter name"); 直到我在variable == JOptionPane.showInputDialog("Enter name");周围添加while(number == null || number.equals("")) ,才会出现此错误variable == JOptionPane.showInputDialog("Enter name"); lines in my code. 我的代码中的行。 Is there any way to handle this error while keeping my added code in? 有什么办法,同时保持我添加的代码在处理这个问题?

    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); }  


    }
    }

This line 这条线

String firstLine, secondLine, thirdLine, number = " ";

equals to 等于

String firstLine;
String secondLine;
String thirdLine;
String number = " ";

So you need to initialize your firstLine, secondLine, thirdLine: 因此,您需要初始化firstLine,secondLine,thirdLine:

String firstLine = "";
String secondLine = "";
String thirdLine = "";
String number = " ";

Adding the while loop around the place where you set the variable means that if the condition is never met, the variables will not receive a value. 添加while围绕在您设定的变量的地方循环意味着,如果条件永远都无法满足时,变量将不会收到值。

But those while loops are wrong as they are. 但是那些while循环本身是错误的。 In general, while loops should not have a condition that waits for something that is not changed inside them. 在一般情况下, while循环不应该有一个等待的东西,是不是里面他们改变的条件下。 Since number is a local variable, there is no other thread that will change it, and it doesn't change inside the loop itself: 由于number是一个局部变量,没有其他线索,将改变它,它不会在循环自身内部改变:

      while(number == null || number.equals("")) 
      {
          firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
      }

I'm pretty sure you wanted to actually make this condition: 我敢肯定,您确实想做这个条件:

      while(firstLine == null || firstLine.equals("")) 
      {
          firstLine = JOptionPane.showInputDialog("Enter name");// prompts for input and displays "Enter Name"
      }

So you should correct that. 因此,您应该纠正它。 Nevertheless, the compiler may still not be happy with that, so you should, indeed, supply a default value when you declare the variable, and as the other answer told you, the declaration: 尽管如此,编译器可能仍然不会满意了,所以你应该,事实上,提供一个默认值,当你声明变量,并作为其他答案告诉你,以下声明:

String firstLine, secondLine, thirdLine, number = " ";

Does not set all the variables to " " - only the number variable. 不要将所有变量都设置为" " -仅将number变量设置为。 You need to assign to each of them separately. 您需要单独分配给每个人。

The value that you set should not be " " (a space). 您设置应该是值" " (空格)。 Because that doesn't meet either of the conditions (it's not null and it's not empty) so it will not go inside the loops, and you'll wonder why you're just getting spaces. 因为这不符合任何一个条件(这不是零和它不是空的),所以它不会去的环内,你会想知道为什么你刚刚的空间。 You should set it to either null or an empty string. 您应该将其设置为null或空字符串。

Let me answer your question with a similar problem: 我来回答类似的问题你的问题:

int x;

while(false) {
    x = 10;
}

System.out.println(x);

Because there is no guarantee that you enter the while loop, there is therefore no guarantee that x is initialized. 因为有你进入while循环没有保障 ,因此存在不能保证 x被初始化。

The same problem occurs in your code. 在你的代码出现同样的问题。 Even if you are logically convinced that you will enter the while loop , the compiler needs to be certain. 即使在逻辑上确信您将进入while loop ,编译器也必须确定。

As such, please initialize your variables. 因此,请初始化变量。 This doesn't initialize them the way you think it does. 这不会对它们进行初始化你认为它的方式。 It, infact, only initializes the last variable. 它,事实上,只有初始化最后一个变量。

String firstLine, secondLine, thirdLine, number = " ";

Try this: 尝试这个:

String firstLine, secondLine, thirdLine, number;
number = " ";
firstLine = null;
secondLine = null;
thirdLine = null;

You can also do this: 您也可以这样做:

String firstLine, secondLine, thirdLine, number;
number = " ";
firstLine = secondLine = thirdLine = null;

Note that you don't have to initialize your variables to null : you could initialize them to any other String as well. 请注意,您不必将变量初始化为null :也可以将它们初始化为任何其他String

只是初始化三个变量为空字符串.. = firstLine中无效,二线= NULL,thirdLine = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM