Results 1 to 4 of 4
- 02-24-2009, 04:47 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
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
- 02-24-2009, 06:15 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-24-2009, 06:16 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,542
- Rep Power
- 11
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.
- 02-24-2009, 06:27 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
java.lang.StackOverflowError Exception
By Marcus in forum Web FrameworksReplies: 4Last Post: 08-24-2012, 10:02 PM -
java.lang.StackOverFlowError exception
By jayaj in forum NetBeansReplies: 1Last Post: 06-08-2008, 11:17 AM -
java.lang.NumberFormatException: For input stream:jav
By osval in forum New To JavaReplies: 2Last Post: 08-07-2007, 03:50 PM -
java.lang.NoClassDefFoundError Exception when I invoke to a class outside projectEJB
By Daniel in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-06-2007, 06:08 AM -
java.lang.NumberFormatException: null
By Eric in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-05-2007, 05:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks