我需要能够将数组分配给 .txt 文件,因此我需要在 while 循环之外引用变量“s”。即使在我定义并初始化变量之后,当我在 while 循环中初始化时仍然会出现错误。我究竟做错了什么?
package vp.sga_form_generator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class GUI extends JFrame{
public GUI() throws FileNotFoundException {
super("SGA Form Creator - Viper Productions");
setSize(1000,800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(30, 2));
//Opens File
Scanner names = new Scanner(new File("names.txt"));
// String name1 = names.next();
String[] s;
while(names.hasNext()){
s = {names.next()};
}
JComboBox names1 = new JComboBox(s);
JComboBox names2 = new JComboBox(s);
add(names1);
add(names2);
}
}