- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我从客户端向服务器发送值时,我收到如下异常。服务器中的语句 dString = new Date().toString(); 正在执行,而不是处理数据报包。请提供您的输入来纠正服务器或我的客户端以获得准确的输出。
Exception in thread "Thread-2" java.lang.NumberFormatException: For input string: "45.0Feb 11 03:35:27 CST 2016"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at com.loan.emi.QuoteServerThread.run(QuoteServerThread.java:75)
at java.lang.Thread.run(Unknown Source)
.//服务器类
public class QuoteServerThread extends JFrame implements Runnable {
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreQuotes = true;
private JTextArea jta = new JTextArea();
public QuoteServerThread() throws IOException {
this("QuoteServerThread");
}
public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(8300);
add(new JScrollPane(jta), BorderLayout.CENTER);
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Server");// It is necessary to show the frame here
try {
in = new BufferedReader(new FileReader("one-liners.txt"));
} catch (FileNotFoundException e) {
jta.append("Couldn't open quote file. Serving time instead.");
}
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// Receive request for processing
DatagramPacket packet
= new DatagramPacket(buf, buf.length);
socket.receive(packet);
// Figure out response
String dString = null;
if (in == null) {
dString = new Date().toString();
} else {
dString = getNextQuote();
}
buf = dString.getBytes();
// Send the response to the client
// at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(
buf, buf.length, address, port);
socket.receive(packet);
double rate = Double.parseDouble(new String(buf).trim());
socket.send(packet);
socket.receive(packet);
double years = Double.parseDouble(new String(buf).trim());
socket.send(packet);
socket.receive(packet);
double loan = Double.parseDouble(new String(buf).trim());
socket.send(packet);
double monthlyPayment = loan * (rate / 1200) /
(1 - (Math.pow(1 / (1 + (rate / 1200)), years * 12)));
socket.send(packet);
double totalPayment = monthlyPayment * years * 12;
socket.send(packet);
jta.append("Interest Rate is " + rate + '\n');
jta.append("Number Of years " + years + '\n');
jta.append("Loan Amount is " + loan + '\n');
jta.append("Monthly payment " + monthlyPayment + '\n');
jta.append("Total Payment " + totalPayment + '\n');
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
protected String getNextQuote() {
String returnValue = null;
try {
if ((returnValue = in.readLine()) == null) {
in.close();
moreQuotes = false;
returnValue = "No more quotes. Goodbye.";
}
} catch (IOException e) {
returnValue = "IOException occurred in server.";
}
return returnValue;
}
}
。//客户端类
public class DgClient extends JFrame {
// Text field for receiving radius
private JTextField jtf = new JTextField();
JTextField interestRate = new JTextField();
JTextField numberOfYears = new JTextField();
JTextField loanAmount = new JTextField();
JButton submit = new JButton("Submit");
// Text area to display contents
private JTextArea jta = new JTextArea();
// Datagram socket
private DatagramSocket socket;
// The byte array for sending and receiving datagram packets
private byte[] buf = new byte[256];
// Server InetAddress
private InetAddress address;
// The packet sent to the server
private DatagramPacket sendPacket;
// The packet received from the server
private DatagramPacket receivePacket;
public static void main(String[] args) {
new DgClient();
}
public DgClient() {
// Panel p to hold the label and text field
JPanel fieldsPanel = new JPanel();
fieldsPanel.setLayout(new GridLayout(3, 3));
fieldsPanel.add(new JLabel("Anual Interest Rate"));
fieldsPanel.add(interestRate);
fieldsPanel.add(new JLabel("Number Of Years"));
fieldsPanel.add(numberOfYears);
fieldsPanel.add(new JLabel("Loan Amount"));
fieldsPanel.add(loanAmount);
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(fieldsPanel);
topPanel.add(submit);
add(topPanel, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
submit.addActionListener(new ButtonListener());
setTitle("Loans Client");
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try {
// get a datagram socket
socket = new DatagramSocket();
address = InetAddress.getByName("localhost");
sendPacket
= new DatagramPacket(buf, buf.length, address, 8300);
receivePacket = new DatagramPacket(buf, buf.length);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
// Initialize buffer for each iteration
Arrays.fill(buf, (byte) 0);
double rate = Double.parseDouble(interestRate.getText().trim());
double years = Double.parseDouble(numberOfYears.getText().trim());
double amount = Double.parseDouble(loanAmount.getText().trim());
sendPacket.setData(new Double(rate).toString().getBytes());
socket.send(sendPacket);
sendPacket.setData(new Double(years).toString().getBytes());
socket.send(sendPacket);
sendPacket.setData(new Double(amount).toString().getBytes());
socket.send(sendPacket);
socket.receive(receivePacket);
double monthlyRepayment = Double.parseDouble(new String(buf).trim());
socket.receive(receivePacket);
double totalAmount = Double.parseDouble(new String(buf).trim());
jta.append("Interest Rate = " + rate + '\n');
jta.append("Number of years = " + years + '\n');
jta.append("Loan Amount = " + amount + '\n');
jta.append("Monthly Repayment = " + monthlyRepayment + '\n');
jta.append("Total Amount = " + totalAmount + '\n');
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
有人可以帮助我是什么导致了错误。我猜它是从客户端向服务器发送空值。但我不知道原因。
最佳答案
您没有正确从 DatagramPacket
中提取数据。您应该使用 new String(buf)
而不是
new String(packet.getData(), packet.getOffset(), packet.getLength())
它发生在任何地方。
I guess it is sending null values
此处没有明显的空值。
关于java - NumberFormatException 解析为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345308/
我正在尝试使用 Integer.parseInt(simNumber) 将 String 转换为 int,但我收到错误消息。如何将 String 转换为 int?我在网上搜索了一下,找到了Intege
每次我尝试启动我的代码时,我总是遇到同样的错误: java.lang.NumberFormatException: For input string: "x" at java.lang.Numbe
在检查我的错误处理代码时偶然发现了这个。当您调用 Integer.parseInt(null) 时,Java 抛出 NumberFormatException 而 Float.parseFloat(n
如果有人可以看看我在 Selenium 方面遇到的问题。我尝试使用以下方法访问上述计算器中的结果: public String getAttributeValue(WebElement element
嗯,我很无聊,所以我想做一个二进制>十进制转换器。我将转换器的上限设置为 30 位数字 (1073741823) 或 (0111111111111111111111111111111)。我遇到的问题是
在以下代码中,Clojure (1.2) 打印出错误消息: (try (let [value "1,a"] (map #(Integer/parseInt %) (.split value
String str="123456.7855456677"; ParsePosition parsePosition = new ParsePosition(0); NumberFormat num
代码: String myVar = "1255763710960"; int myTempVar=0; try { myTempVar = Integer.valueOf(myVar); }
我在NumberFormatException代码中收到Grails错误,该错误是将电影分类到数据库中。该错误表明它来自cron插件。我已经完成了研究,并且一直在尝试使用NumberFormatExc
我在将字符串解析为原语时遇到问题。 File file1 = new File("bankData.txt"); Scanner sc = new Scanner(fi
所以我试图从日期列表中查找最新日期,但我不断收到 NumberFormatException。有什么办法可以解决这个问题吗? import java.util.*; public class Date
如果使用变量 bin1 它不会转换,但是如果我用 bin2 替换参数,它似乎可以工作。 我尝试使用 long 而不是 int。还是一样。 public class Test{ public s
我正在尝试找出如何在代码中捕获数字格式异常错误,这样如果用户在字符串中输入字母并且我的程序尝试将其解析为 int 我的程序将不会抛出错误而是停止并返回一个 boolean 值。我还试图了解,如果 tr
该程序的本质目的是将学生信息写入文件并从同一文件中读取。该程序包括一个 if 语句,用于判断学生是否处于良好信誉或留校察看状态,每个语句都有各自的文件(goodstand.txt 和 probatio
我正在用 Python/WLST 编写代码,以自动连接、启动和停止 weblogic 中的托管服务器。当我启动 Python 时,出现以下错误。 异常的含义是什么?我该如何解决它? Initializ
这是我的代码: public void setAreaAccessPoints(){ String mac = "",essid = "",status = ""; int stre
我正在用 Java 开发一个应用程序,发现了这个奇怪的行为: 如果通过控制面板将区域设置格式设置为匈牙利语(系统默认值),我会收到此异常,但如果我将其设置为英语格式,则它可以正常工作。也适用于虚拟 M
我正在尝试制作一个计算器应用程序,但不幸的是我无法解析字符串。 应用程序接受输入并将其显示在 EditText et2 中。当按下“等于”键时,计算器将执行以下工作。 et2 = (EditText)
我有一个文件,其中包含许多电影标题及其年份、5 星评级和长度。我正在尝试读取该文件并将标题、年份、评级和长度存储为变量。我遇到的问题是在我的检索年份的代码中。该代码可以编译,但在运行时,当它到达具有多
我正在解决亚马逊 Interview Street 网站上的 Connected Sets 问题 https://amazon.interviewstreet.com/challenges我的代码对于
我是一名优秀的程序员,十分优秀!