我正在创建一个每日保存日志。创建时,我得到了这个NumberFormatException,我需要一些帮助来完成这个项目。
这是我正在研究的代码。
package app;
import java.awt.EventQueue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JSeparator;
import javax.swing.border.MatteBorder;
public class Application {
BufferedWriter log = null;
BufferedWriter bal = null;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Appilcation window = new Appilcation();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Appilcation() {
try {
initialize();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
* @throws IOException
*/
@SuppressWarnings("resource")
private void initialize() throws IOException,NumberFormatException{
frame = new JFrame();
frame.setUndecorated(true);
frame.setBounds(100, 100, 450, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
log= new BufferedWriter(new FileWriter(new File("Log.txt")));//writer for log
bal= new BufferedWriter(new FileWriter(new File("bal.txt")));// " " bal
Scanner File = new Scanner(new File("bal.txt"));
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Date date = new Date();
JPanel panel = new JPanel();
panel.setBackground(new Color(255, 0, 102));
panel.setBounds(0, 0, 450, 420);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblDdmmyyyy = new JLabel("dd/mm/yyyy");
lblDdmmyyyy.setFont(new Font("ARCADE", Font.PLAIN, 16));
lblDdmmyyyy.setHorizontalAlignment(SwingConstants.CENTER);
lblDdmmyyyy.setHorizontalTextPosition(SwingConstants.CENTER);
lblDdmmyyyy.setBounds(10, 11, 100, 29);
panel.add(lblDdmmyyyy);
JLabel lblHh = new JLabel("hh:mm:ss");
lblHh.setFont(new Font("ARCADE", Font.PLAIN, 16));
lblHh.setHorizontalAlignment(SwingConstants.CENTER);
lblHh.setBounds(324, 11, 100, 29);
panel.add(lblHh);
JButton btnAddSavings = new JButton("Add");
btnAddSavings.setBackground(new Color(255, 255, 0));
btnAddSavings.setFont(new Font("ARCADE", Font.PLAIN, 16));
btnAddSavings.addActionListener(new ActionListener() {
//something is not ryt here
public void actionPerformed(ActionEvent arg0) {
String Add=JOptionPane.showInputDialog(panel,"Enter Amount to be added");
String s=/*sdf.format(date)+**/" "+Add+"(Added)";
String Bal="";
while(File.hasNextLine())
Bal=File.nextLine();
int aa=Integer.parseInt(Bal);
int bb =Integer.parseInt(Add);
int sum=aa+bb;
try {
bal.write(sum+"\n");
log.write(s+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnAddSavings.setBounds(138, 150, 173, 64);
panel.add(btnAddSavings);
JButton btnRemove = new JButton("Remove");
btnRemove.setBackground(new Color(255, 127, 80));
btnRemove.setFont(new Font("ARCADE", Font.PLAIN, 16));
btnRemove.setBounds(138, 225, 173, 64);
panel.add(btnRemove);
JButton btnIdeas = new JButton("ideas");
btnIdeas.setBackground(new Color(153, 204, 255));
btnIdeas.setFont(new Font("ARCADE", Font.PLAIN, 16));
btnIdeas.setBounds(138, 300, 173, 34);
panel.add(btnIdeas);
JButton btnExit = new JButton("Log");
btnExit.setBackground(new Color(204, 204, 255));
btnExit.setBounds(138, 345, 71, 23);
panel.add(btnExit);
JButton btnExit_1 = new JButton("Exit");
btnExit_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit_1.setBackground(new Color(204, 204, 255));
btnExit_1.setBounds(240, 345, 71, 23);
panel.add(btnExit_1);
JLabel lblRs = new JLabel("0 Rs.");
lblRs.setBackground(new Color(0,0,0,0));
lblRs.setFont(new Font("Bebas", Font.PLAIN, 23));
lblRs.setHorizontalAlignment(SwingConstants.CENTER);
lblRs.setBounds(105, 81, 239, 58);
panel.add(lblRs);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
panel_1.setBackground(new Color(0,0,0,0));
panel_1.setBounds(105, 81, 239, 58);
panel.add(panel_1);
bal.close();
log.close();
}
}
和错误消息:
线程“AWT-EventQueue-0”Java.lang.NumberFormatException.forInputString(NumberFormatException.Java.base:68)在Java.base/Java.lang.Integer.ParseInt(Integer.Java:668)在Java.base/Java.lang.Integer.ParseInt(Integer.Java:776)在app.appilcation$2.ActionPerformed(appilcation.Java.113)在Java.deskto/Java.swing.abstractButton.fireActionPerformed.awt.component.processEvent(component.java:6397)在java.desktop/java.awt.container.processEvent(conponent.java:2263)在java.desktop/java.awt.component.dispatchEventImpl(component.java:5008)在java.desktop/java.awt.container.dispatchEventImpl(component.java:2321)在java.desktop15)在java.base/java.security.AccessController.DoPrivileged(AccessController.java:389)在java.base/java.security.ProtectionDomain$javaSecurityAccessImpl.DoIntersectionPrivileged(ProtectionDomain.java:85)在java.base/java.security.ProtectionDomain$javaSecurityAccessImpl.DoIntersectionPrivileged(ProtectionDomain.java:95)在.Java:113)Java.desktop/Java.awt.EventDispatchThread.PumpeVents(EventDispatchThread.Java:109)Java.desktop/Java.awt.EventDispatchThread.PumpeVents(EventDispatchThread.Java:101)Java.desktop/Java.awt.EventDispatchThread.Run(EventDispatchThread.Java:90)
我尝试过的事情:
->确保所有文件都不为空。
请帮帮忙。
下面的片段中有几个问题与未经验证的输入有关
add
从joptionpane
bal
可能为空:实际上bal
被分配到文件的最后一行。因此,file可能是非空的,但它可能包含一个空行,因此bal
是“”
; String Add=JOptionPane.showInputDialog(panel,"Enter Amount to be added");
String s=/*sdf.format(date)+**/" "+Add+"(Added)";
String Bal="";
while(File.hasNextLine())
Bal=File.nextLine();
int aa=Integer.parseInt(Bal);
int bb =Integer.parseInt(Add);
int sum=aa+bb;
因此您可能希望实现一个单独的方法来顺利解析数字:
java prettyprint-override"> private static int getNumber(String s) {
int res = 0;
try {
res = Integer.parseInt(s);
} catch (Exception ex) {
System.err.println("Failed to parse number from '" + s + "', " + ex.getMessage());
res = 0;
}
return res;
}
// ...
int aa = getNumber(Bal);
System.out.printf("DEBUG: Bal='%s' -> aa=%d%n", Bal, aa);
int bb = getNumber(Add);
System.out.printf("DEBUG: Add='%s' -> bb=%d%n", Add, bb);
问题似乎是一个文件有一个空行。在解析Bal之前,检查它是否为空。
我试图解决codechef问题,我能够在IDE中获得输出,也可以使用自定义输入,当我尝试使用这些输入运行时,它会给我错误 问题链接:https://www.codechef.com/problems/HS08TEST 代码: 错误: 异常线程"main"java.util.NoSuchElement异常java.util.Scanner.throwFor(Scanner.java:862)java
我在运行时遇到一个错误。使用if语句检查是否为空时会导致此错误。
我试图执行以下nodejs/mongoDB代码: 符号很好地注销到控制台,但在此之后,代码抛出一个错误“TypeError:Cannotread property”symbol“of null”。 代码在执行tickers.push部分时似乎忘记了doc.symbol。我该怎么解决这个问题? 更新: 我试着移动控制台。将(股票代码)记录到光标中。每次回调,它都会打印出每次迭代所需的数组,因此符号推
问题内容: 代码是: 而logcat错误是: 03-18 03:19:07.847:E / AndroidRuntime(875):由以下原因引起:java.lang.NumberFormatException:无效的float:“” 问题答案: 好像editText2中的String为空,因此无法将其解析为float。 一种可能的解决方案是先检查String是否为空,然后确定默认值,另一种方法是
我试图在网页上打印错误loginform.html但th: each="e:${#fields.errors('email')}"th: text="${e}" 这是我的loginform.html代码 控制器代码: 使用者java代码: 错误是:此应用程序没有/Error的显式映射的白标签错误页,所以您将其视为一种回退。