简体   繁体   中英

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); lines with variable being firstLine, secondLine & thirdLine. This error didn't appear until I added while(number == null || number.equals("")) around the 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:

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.

But those while loops are wrong as they are. In general, while loops should not have a condition that waits for something that is not changed inside them. Since number is a local variable, there is no other thread that will change it, and it doesn't change inside the loop itself:

      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. 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.

Let me answer your question with a similar problem:

int x;

while(false) {
    x = 10;
}

System.out.println(x);

Because there is no that you enter the while loop, there is therefore no that x is initialized. ,因此存在不能 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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