Results 1 to 4 of 4
  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Exception java.lang.NumberFormatException

    hi,

    In my programming I am reading data from a file. The data contains strings, int, int ... type of data. Using this data I have to create an object. At the time of compilation it is not showing any error. At the time running it is showing the following exception:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "Error and attack tolerance of complex networks"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at SocialNetworkConstruction.createArticleNode(Main1. java:96)
    at Main1.main(Main1.java:20)

    Whatever data I am collected from a file I am converting into proper type. Then only I am passing that data for the constructor to create object. Even though it is raising exception. The code is as follows:

    Main1.java

    import jsns.model.CommunicationNetworkId;
    import jsns.model.util.ContinuousTime;
    import jsns.model.util.LoggingMasterImpl;
    import jsns.model.IEnvironment;
    import jsns.model.util.EnvironmentImpl;
    import jsns.system.SimulationControl;
    import java.lang.String;
    import java.io.*;
    import java.util.*;

    public class Main1
    {
    public static void main(String args[]) throws IOException
    { // calling the function of subclasses
    StoreDataInAFile fileObj=new StoreDataInAFile();
    SocialNetworkConstruction network=new SocialNetworkConstruction();
    String data[]=fileObj.readDataFromAFile(); // collecting the data which is read from file
    for(int i=0;i<data.length;i++)
    {
    int index=network.createArticleNode(data,i); // passing that data to a function
    System.out.println("The entered node is stored in network with index number "+index);
    }
    }
    }

    class StoreDataInAFile // class for reading the data from a file
    {
    public String[] readDataFromAFile()
    {
    String arr[]=null;
    try
    {
    BufferedReader br = new BufferedReader(new FileReader("PrintOutput.txt"));
    ArrayList Hits = new ArrayList();
    String line;
    while ((line = br.readLine()) != null)
    {
    String tkn[] =line.split(",");
    for(int i=0;i<tkn.length;i++)
    Hits.add(tkn[i]);
    }
    arr=new String[Hits.size()];
    arr=(String[])Hits.toArray(arr);
    }
    catch(Exception e)
    {
    System.out.println("Exception: " + e);
    e.printStackTrace();
    }
    return arr;
    }
    }

    class SocialNetworkConstruction // construction of a network.
    { // these are the elements declared in the jar file which I am using for my project
    CommunicationNetworkId[] networkIds = { new CommunicationNetworkId("Academics") };
    AcademicsAgents agent[]=new AcademicsAgents[1000]; // user defined class
    Author author[]=new Author[5000]; // user defined class
    static int m=0,n=0;
    int p;
    int neighborsList[][]=new int[1000][1000];
    int weightBetweenAuthors[][]=new int[1000][1000];
    int weightBetweenAuthorNArticle[][]=new int [1000][1000];
    int authorsId[]=new int [20];

    ContinuousTime startTime = new ContinuousTime(0); // from jar file
    ContinuousTime endTime = new ContinuousTime(10); // from jar file
    LoggingMasterImpl logging=new LoggingMasterImpl(); // from jar file

    SimulationControl simulation = new SimulationControl(startTime, logging); // from jar file
    IEnvironment env=new EnvironmentImpl(); // from jar file

    public int checkNodeIdInNetwork(int mn, String id)
    { // checking before the node creation whether it is already exist or not
    for(p=0;p<mn;p++)
    {
    String s=agent[p].getId();
    if(id.equalsIgnoreCase(s)) // comparing with the previous nodes for existance
    {
    System.out.println("The entered node ia already in the network with the agent index "+p);
    return p; // returning th index if the node with that data already exists
    }
    }
    return -1;
    }
    public int createArticleNode(String[] tkn,int a)
    {
    int index=checkNodeIdInNetwork(m,tkn[a]);
    if(index == -1)
    { // Converting the data to its appropriate type.
    String name=tkn[a];
    int cite=Integer.parseInt(tkn[a+1]);
    int yr=Integer.parseInt(tkn[a+2]);
    int a_count=Integer.parseInt(tkn[a+3]); // Showing error at this line
    String ref=tkn[a+4];
    agent[m]=new AcademicsAgents(name,cite,yr,a_count,ref); // creating the node
    simulation.addAgent(agent[m], networkIds); // adding to the network
    index=checkNodeIdInNetwork(m,tkn[a+4]); // checking the last string existance
    if(index != -1)
    simulation.addDirectedConnection(agent[m].getId(), agent[index].getId(), networkIds[0]); // establishing a connection
    else
    System.out.println("Sorry, the entered reference articles is not exist in the defined network.");
    for(int i=0;i<agent[m].getNoOfAuthors();i++)
    {
    }
    return (++m); returning the updated node index
    }
    else
    {
    System.out.println("No need to add in a network.");
    return index;
    }
    }
    }

    AcademicsAgents.java

    import jsns.model.AbstractAgent;
    import jsns.model.IAgentActor;
    import jsns.model.IAgentSensor;
    import java.util.*;
    import java.io.*;

    public class AcademicsAgents extends AbstractAgent implements Serializable
    {
    private int citations;
    private int numberOfAuthors;
    private int publishingYear;
    private String referencedArticle;

    public AcademicsAgents(String id, int cite,int year,int authorsCount,String reference)
    { // constructor
    super(id);
    citations=cite;
    publishingYear=year;
    numberOfAuthors=authorsCount;
    referencedArticle=reference;
    }

    // set and get methods

    .....
    }

    Author.java

    import java.util.*;
    import jsns.model.AbstractAgent;
    import jsns.model.IAgentActor;
    import jsns.model.IAgentSensor;
    import jsns.model.IEnvironment;
    import jsns.model.util.EnvironmentImpl;
    import jsns.model.CommunicationNetworkId;
    import jsns.model.ISimTime;
    import jsns.model.util.GenericCommunicationObject;
    import jsns.model.util.ContinuousTime;
    import jsns.model.ILoggingMaster;
    import jsns.model.util.LoggingMasterImpl;

    public class Author extends AbstractAgent
    {
    private int noOfArticlesPublished;
    private int noOfArticlesPublishedInNetwork;
    private String workingPlace;
    private String department;
    private double weight;
    private int status;
    private int Degree;
    static int i=1;

    private ContinuousTime delay = new ContinuousTime(1);

    public Author(String id,int count1,int count2,String location,String dept,double w,int stat,int degree)
    {
    super(id);
    noOfArticlesPublishedInNetwork=count1;
    noOfArticlesPublished=count2;
    workingPlace=location;
    department=dept;
    weight=w;
    status=stat;
    Degree=degree;
    }
    // get and set methods definitions
    ....
    }

    can you help me regarding this.

    With Sincere Regards,
    Vasavi

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well without pouring over all your code here, I can see that "Error and attack tolerance of complex networks" is not a number, and so a NUmberFormatException is perfectly reasonable. My guess is the file you are reading does not conform to the assumptions you've made about it.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The error message means that you are trying to parse the String "Error and attack tolerance of complex networks" as if it were a number.

    Your readDataFromAFile() method is currently creating a huge String array from the file PrintOutput.txt. The format of this file is incorrect and, as a result, one of the entries in this array is the String "Error and attack tolerance of complex networks" at a place that should contain a numeral.

    You could look at the file and try and find where this string is (wrongly) located. But better might be to abandon String in favour of a class that better represents the hits. In particular you could use a class that returns data of a specific type (String or int) at the time you need that type.

  4. #4
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Exception java.lang.NumberFormatException

    Hi,

    Thank you guys. The stored columns are just reversed. Instead of storing a int I stored string and in place of string I stored int. Because of that it is giving such type of error. Again checking the file I found the error.

    Thank you all.

Similar Threads

  1. Replies: 2
    Last Post: 01-20-2014, 07:45 AM
  2. java.lang.StackOverflowError Exception
    By Marcus in forum Web Frameworks
    Replies: 4
    Last Post: 08-24-2012, 10:02 PM
  3. java.lang.StackOverFlowError exception
    By jayaj in forum NetBeans
    Replies: 1
    Last Post: 06-08-2008, 11:17 AM
  4. Replies: 2
    Last Post: 08-07-2007, 03:50 PM
  5. java.lang.NumberFormatException: null
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •