Java 读取同一行中的多个字符串
好了,伙计们,我一直得到一个没有这样的元素异常代码,并且不知道如何调试它。我很确定问题出在我的可实例化类中Java 读取同一行中的多个字符串,java,string,java.util.scanner,Java,String,Java.util.scanner,好了,伙计们,我一直得到一个没有这样的元素异常代码,并且不知道如何调试它。我很确定问题出在我的可实例化类中 list.add(new Fan( scanFile.nextInt(), scanFile.nextLine(), scanFile.nextLine(), scanFile.nextLine() )); 在下面编辑了我的代码,现在可以使用了 有没有办法克服这个问题 public class Fan { //fields private stat
list.add(new Fan(
scanFile.nextInt(),
scanFile.nextLine(),
scanFile.nextLine(),
scanFile.nextLine()
));
在下面编辑了我的代码,现在可以使用了
有没有办法克服这个问题
public class Fan {
//fields
private static final String fileName = "fans.txt";
private String name;
private String email;
private String promo;
private int age;
//constructor
private Fan(int age, String name, String email,String promo){
this.name=name;
this.email=email;
this.age=age;
this.promo = promo;
}
//get name
public String getName() {
return name;
}
//get age
public int getAge() {
return age;
}
//get email
public String getEmail() {
return email;
}
//get subscription
public String getPromo(){
return promo;
}
//get existing Fan Information
public static ArrayList<Fan> getSubscribingFans() {
//local variables
File FanFile = new File(fileName);
ArrayList<Fan> list = new ArrayList<>();
//create file if it doesn't exist
if (!FanFile.exists()) {
try {
FanFile.createNewFile();
return list;
} catch (IOException ioe) {
return null;
}
}
//read file, save each line as an Fan object, store in ArrayList
try (Scanner scanFile = new Scanner(FanFile)) {
//edited now works
while (scanFile.hasNext()) {
list.add(new Fan(scanFile.nextInt(), scanFile.nextLine(),null,null));
}
} catch (IOException ioe) {
return null;
}
//return list
return list;
}
//add a new Fan
public static boolean addFanSubscriber(int age, String name, String email, String promo) {
//append to file
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName, true))) {
//edited spacing inside qoutes
pw.println(age + " " + name + " "+ email + " " + promo);
} catch (IOException ioe) {
return false;
}
return true;
}
}
公共类风扇{
//田地
私有静态最终字符串fileName=“fans.txt”;
私有字符串名称;
私人字符串电子邮件;
私人字符串促销;
私人互联网;
//建造师
私人风扇(整数、字符串名称、字符串电子邮件、字符串促销){
this.name=name;
this.email=电子邮件;
这个。年龄=年龄;
this.promo=promo;
}
//得名
公共字符串getName(){
返回名称;
}
//变老
公共整数getAge(){
回归年龄;
}
//收到电子邮件
公共字符串getEmail(){
回复邮件;
}
//获得订阅
公共字符串getPromo(){
退货促销;
}
//获取现有风扇信息
公共静态ArrayList getSubscribingFans(){
//局部变量
文件FanFile=新文件(文件名);
ArrayList=新建ArrayList();
//创建不存在的文件
如果(!FanFile.exists()){
试一试{
FanFile.createNewFile();
退货清单;
}捕获(ioe异常ioe){
返回null;
}
}
//读取文件,将每行另存为风扇对象,存储在ArrayList中
try(Scanner scanFile=新扫描仪(FanFile)){
//现在编辑工作
while(scanFile.hasNext()){
添加(新风扇(scanFile.nextInt(),scanFile.nextLine(),null,null));
}
}捕获(ioe异常ioe){
返回null;
}
//返回列表
退货清单;
}
//添加一个新风扇
公共静态布尔addFanSubscriber(整数、字符串名称、字符串电子邮件、字符串促销){
//附加到文件
try(PrintWriter pw=new PrintWriter(new FileWriter(fileName,true))){
//qoutes内编辑的间距
打印项次(年龄+姓名+电子邮件+促销);
}捕获(ioe异常ioe){
返回false;
}
返回true;
}
}
这是我的JFrame代码
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FanControl frame = new FanControl();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FanControl() {
txtEmail.setHorizontalAlignment(SwingConstants.CENTER);
txtEmail.setColumns(8);
txtName.setHorizontalAlignment(SwingConstants.CENTER);
txtName.setColumns(8);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 484, 326);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
panel.setBackground(Color.BLACK);
contentPane.add(panel, BorderLayout.NORTH);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Monotype Corsiva", Font.BOLD, 36));
lblNewLabel.setForeground(new Color(0, 255, 255));
panel.add(lblNewLabel);
panel_1.setBackground(Color.BLACK);
contentPane.add(panel_1, BorderLayout.SOUTH);
lblAge.setFont(new Font("Tahoma", Font.BOLD, 11));
lblAge.setForeground(Color.CYAN);
lblAge.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.add(lblAge);
spnAge.setModel(new SpinnerNumberModel(18, 0, 100, 1));
panel_1.add(spnAge);
lblName.setFont(new Font("Tahoma", Font.BOLD, 11));
lblName.setForeground(Color.CYAN);
lblName.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.add(lblName);
panel_1.add(txtName);
lblEmail.setFont(new Font("Tahoma", Font.BOLD, 11));
lblEmail.setForeground(Color.CYAN);
lblEmail.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.add(lblEmail);
panel_1.add(txtEmail);
btnSubmit.addActionListener(new BtnSubmitActionListener());
btnSubmit.setBackground(Color.BLACK);
btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 11));
btnSubmit.setForeground(Color.GREEN);
panel_1.add(btnSubmit);
scrollPane.setBorder(null);
contentPane.add(scrollPane, BorderLayout.CENTER);
scrollPane.setViewportView(textArea);
panel_2.setBorder(null);
panel_2.setBackground(Color.BLACK);
scrollPane.setRowHeaderView(panel_2);
panel_2.setLayout(new GridLayout(0, 1, 0, 0));
lblWouldYouLike.setBackground(Color.BLACK);
lblWouldYouLike.setForeground(Color.CYAN);
lblWouldYouLike.setFont(new Font("Tahoma", Font.BOLD, 11));
panel_2.add(lblWouldYouLike);
buttonGroup.add(btnYes);
buttonGroup.add(btnNo);
btnYes.setBackground(Color.BLACK);
btnYes.setFont(new Font("Tahoma", Font.BOLD, 11));
btnYes.setForeground(Color.GREEN);
panel_2.add(btnYes);
btnNo.setBackground(Color.BLACK);
btnNo.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNo.setForeground(Color.RED);
btnNo.setSelected(true);
panel_2.add(btnNo);
}
//update text area with current list of inventory items
private void updateList() {
//clear text area
textArea.setText("");
//get current list
ArrayList<Fan> fans = Fan.getSubscribingFans();
if (fans == null) {
JOptionPane.showMessageDialog(contentPane, "ERROR: unable to create or open fans file");
return;
}
//create String array
String[] fansStrs = new String[fans.size()];
for (int i=0; i<fans.size(); i++) {
fansStrs[i] = String.format("%10d %s \n", fans.get(i).getAge(), fans.get(i).getName(), fans.get(i).getEmail(),fans.get(i).getPromo());
}
//sort String array
Arrays.sort(fansStrs);
//display strings
for (String s: fansStrs) {
textArea.append(s);
}
}
//submit button clicked
private class BtnSubmitActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
//local variables
String email = txtEmail.getText();
int age = (int)spnAge.getValue();
String name = txtName.getText();
String promo;
boolean ok = true;
//add fan EDITED promo =
if (btnYes.isSelected()){
promo = "PromoTeam";
}else{
promo = "";
}
ok = Fan.addFanSubscriber(age, name, email, promo);
if (!ok) {
JOptionPane.showMessageDialog(contentPane, "ERROR: unable to add item to inventory file");
return;
}
//refresh the list
updateList();
//clear input fields
txtEmail.setText("");
txtName.setText("");
spnAge.setValue(18);
}
}
publicstaticvoidmain(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
FanControl帧=新的FanControl();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共控制{
txtEmail.setHorizontalAlignment(SwingConstants.CENTER);
txtEmail.setColumns(8);
txtName.setHorizontalAlignment(SwingConstants.CENTER);
txtName.setColumns(8);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100484326);
contentPane=newjpanel();
contentPane.setBackground(颜色:黑色);
setboorder(新的EmptyBorder(5,5,5,5));
setLayout(新的BorderLayout(0,0));
setContentPane(contentPane);
面板.立根背景(颜色.黑色);
添加(面板,BorderLayout.NORTH);
lblNewLabel.SETHORISTALLING(旋转约束中心);
lblNewLabel.setFont(新字体(“Monotype Corsiva”,Font.BOLD,36));
lblNewLabel.setForeground(新颜色(0,255,255));
面板添加(lblNewLabel);
面板1.立根背景(颜色:黑色);
contentPane.add(面板1,BorderLayout.SOUTH);
lblAge.setFont(新字体(“Tahoma”,Font.BOLD,11));
lblAge.setForeground(颜色:青色);
lblAge.setHorizontalAlignment(旋转约束中心);
面板1.添加(lblAge);
setModel(新喷丝头编号模型(18,01001));
面板1.添加(spnAge);
lblName.setFont(新字体(“Tahoma”,Font.BOLD,11));
lblName.setForeground(颜色为青色);
lblName.setHorizontalAlignment(SwingConstants.CENTER);
面板1.添加(lblName);
面板1.添加(txtName);
lblEmail.setFont(新字体(“Tahoma”,Font.BOLD,11));
lblEmail.setForeground(颜色:青色);
lblEmail.setHorizontalAlignment(旋转约束中心);
面板1.添加(lblEmail);
面板1.添加(txtEmail);
btnSubmit.addActionListener(新的BtnSubmitActionListener());
btnSubmit.setBackground(颜色:黑色);
btnSubmit.setFont(新字体(“Tahoma”,Font.BOLD,11));
btnSubmit.setForeground(颜色为绿色);
面板1.添加(btnSubmit);
滚动窗格。设置顺序(空);
添加(滚动窗格,BorderLayout.CENTER);
scrollPane.setViewportView(文本区域);
面板2.订单(空);
面板2.立根背景(颜色:黑色);
scrollPane.setRowHeaderView(面板2);
面板2.设置布局(新的网格布局(0,1,0,0));
lblWouldYouLike.挫折地(颜色.黑色);
lblWouldYouLike.setForeground(颜色:青色);
lblWouldYouLike.setFont(新字体(“Tahoma”,Font.BOLD,11));
面板2.添加(lblWouldYouLike);
按钮组添加(btnYes);
按钮组添加(btnNo);
b.挫折背景(颜色:黑色);
setFont(新字体(“Tahoma”,Font.BOLD,11));
设置前景(颜色为绿色);
面板2.添加(btnYes);
无挫折背景(颜色为黑色);
btnNo.setFont(新字体(“Tahoma”,Font.BOLD,11));
btnNo.设置前景(颜色为红色);
btnNo.setSelected(真);
面板2.添加(btnNo);
}
//使用库存项目的当前列表更新文本区域
私有void updateList(){
//明文区
textArea.setText(“”);
//获取当前列表
ArrayList fans=Fan.getSubscribingFans();
if(fans==null){
showMessageDialog(contentPane,“错误:无法创建或打开风扇文件”);
回来
}
//创建字符串数组
String[]fansStrs=新字符串[fans.size()];
对于(int i=0;i请看以下几行:
while (scanFile.hasNextLine()) {
list.add(new Fan(scanFile.nextInt(), scanFile.nextLine(), scanFile.nextLine(), scanFile.nextLine()));
}
如果输入文件末尾有空行,则while循环的条件为true,但是nextInt
将找不到任何int
,正如Java文档所述:
抛出:
NoTouchElementException-如果输入用尽
资料来源:
如果最后一个数据集不完整,并且您的数据库中没有剩余的三行,则会出现相同的错误