gpt4 book ai didi

JAVA:服务器客户端程序在读取文本文件时出现 NumberFormatException

转载 作者:行者123 更新时间:2023-12-01 11:31:00 26 4
gpt4 key购买 nike

我正在开发一个服务器-客户端程序,其中服务器将书籍信息从文本文件发送到客户端。我在服务器类上遇到运行时 NumberFormatException 错误。我为此拥有的代码:

Server.java

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UDPServer {

private static ArrayList<ReadingMatter> books;

private static String stringUnderQuotes(String s)
{
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(s);
while (m.find()) {
return m.group(1);
}

return null;
}

private static void getdata() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("ReadingMatterialInputFile.txt"));
try {
String category;
while ((category = br.readLine()) != null) {

String title = stringUnderQuotes(br.readLine());
String ISBN = stringUnderQuotes(br.readLine());
double price = Double.parseDouble(br.readLine().split(" ")[1]);

ReadingMatter rm;

if(category.equals("BOOK"))
{
String author[] = stringUnderQuotes(br.readLine()).split(",");
ArrayList<String> a = new ArrayList<String>();
Collections.addAll(a, author);

rm = new Book(title, ISBN, price, a);
}
else if(category.equals("MAGAZINE"))
{
String editor = br.readLine();

rm = new Magazine(title, ISBN, price, editor);
}
else if(category.equals("TEXTBOOK"))
{
String[] author = br.readLine().split(",");
boolean answers = Boolean.valueOf(br.readLine().split(" ")[1]);

ArrayList<String> a = new ArrayList<String>();
Collections.addAll(a, author);

rm = new TextBook(title, ISBN, price, a, answers);
}
else
{
String[] author = br.readLine().split(",");
String[] characters = br.readLine().split(",");

ArrayList<String> a = new ArrayList<String>();
Collections.addAll(a, author);

ArrayList<String> ch = new ArrayList<String>();
Collections.addAll(ch, characters);

rm = new Novel(title, ISBN, price, a, ch);

}

books.add(rm);
}
} finally {
br.close();
}
}

private static String find(String category, String title)
{
Iterator<ReadingMatter> itr = books.iterator();
while (itr.hasNext()) {
ReadingMatter element = itr.next();

if(element.getTitle().equals(title))
{
if(category.equals("BOOK") && element instanceof Book)
{
return element.toString();
}
else if(category.equals("MAGAZINE") && element instanceof Magazine)
{
return element.toString();
}
else if(category.equals("TEXTBOOK") && element instanceof TextBook)
{
return element.toString();
}
else if(category.equals("NOVEL") && element instanceof Novel)
{
return element.toString();
}

}
}

return null;
}

public static void main(String args[]) throws Exception
{
books = new ArrayList<ReadingMatter>();

getdata();

try
{
DatagramSocket serverSocket = new DatagramSocket(9000);

byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

while(true)
{

receiveData = new byte[1024];

DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);

System.out.println ("Waiting for datagram packet");

serverSocket.receive(receivePacket);

String sentence = new String(receivePacket.getData());

String[] data = sentence.split(",");

String category = data[0].trim();
String title = data[1].trim();

System.out.println("Received data: \n");
System.out.println("Category: "+category);
System.out.println("Title: "+title);

String result = null;
if(category == "" || title == "")
{
result = "Category or Title data are not valid !!";
}
else
{
result = find(category, title);

if(result == null)
result = "Sorry !! Data Not found";

}

InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();

System.out.println ("\nReturning Query Result to client: \n\n"+result);



sendData = result.getBytes();

DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);

serverSocket.send(sendPacket);

}

}
catch (SocketException ex) {
System.out.println("UDP Port 9000 is occupied.");
System.exit(1);
}

}
}

书籍类别还有其他类别,例如书籍、杂志、小说等。

它从中读取信息的文本文件如下:

BOOK
TITLE: "A Concise History of Australia"
ISBN: "9780521601016"
PRICE: 29.99
AUTHOR: "Stuart Macintyre"

BOOK
TITLE: "The Bloxworth Blue"
ISBN: "9780060213435"
PRICE: 12.95
AUTHOR: "William Corlett"

现在我在运行服务器时遇到此错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""9780060213435""
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at UDPServer.getdata(UDPServer.java:37)
at UDPServer.main(UDPServer.java:122)

我可以获得一些帮助来找出我做错的地方吗?谢谢大家。

最佳答案

您的错误消息字面上说明了一切。您正在尝试使用引号来格式化文字 "9780060213435" 。你不能在它周围加引号。删除引号,然后进行格式化。

关于JAVA:服务器客户端程序在读取文本文件时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30376753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com