2 Replies - 447 Views - Last Post: 21 May 2015 - 12:32 PM Rate Topic: -----

#1 Durbeen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-April 15

JAVA: Server-Client program getting NumberFormatException reading a te

Posted 21 May 2015 - 09:00 AM

I am working on a Server-Client program where server send Book information from a text file to the client. I'm having run time NumberFormatException error on Server class. The codes I have for this:

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);
        }

    }
}


There are other classes for book category such as Book, Magazine, Novel etc.

And the text file it's reading info from is like:

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"


Now I'm having this error when I run 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)


Can I get some help to locate where I'm doing wrong? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: JAVA: Server-Client program getting NumberFormatException reading a te

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: JAVA: Server-Client program getting NumberFormatException reading a te

Posted 21 May 2015 - 12:15 PM

It looks like there are an extra set of quotes when parsing the string: ""9780060213435"". Those quotes are probably what's throwing off the parse.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: JAVA: Server-Client program getting NumberFormatException reading a te

Posted 21 May 2015 - 12:32 PM

Yes, the quotes are redundant to your parsing, as is (probably) the token 'BOOK'. If the file is regular, then every initial token is superfluous in a sense.
All in all, it's a poor format, but your only real field separator is ':'
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1