JAVA:Server-Client程序获取NumberFormatException读取文本文件

时间:2015-05-21 14:29:23

标签: java networking

我正在开发一个服务器客户端程序,服务器将书籍信息从文本文件发送到客户端。我在Server类上运行时出现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"

现在,当我运行Server时出现此错误:

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)

我可以帮助找到我做错的地方吗?谢谢大家。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:-1)

将第37行替换为:

String number = br.readLine().split(" ")[1];
number = number.substring(1, number.length() - 1);
double price = Double.parseDouble(number );