-
noSuchElement exception!!
it is 4:30. i have been on this program since 6 yesterday afternoon!! help me fix this please!!
it's a NoSuchElementException. so the string is null and it has to do with the nextToken() method.
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:259)
at BankDatabase.readFile(BankDatabase.java:26)
at BankingSystem_hw7.main(BankingSystem_hw7.java:366)
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
when i was in the debugger, i noticed that in the FIRST LINE of the FOR LOOP, the value of inStr in the debugger was all in red. does that signify something?
then, moving on, the first value that is tokenized is firstName. however, when i look up it's value, it says "10". what is going on there?
public boolean readFile(String _dbFileName) throws FileNotFoundException, IOException
{
// open the database file to read
BufferedReader inFile = new BufferedReader (new FileReader (_dbFileName));
String inStr = inFile.readLine();
// the first line is the # of clients
int N = Integer.parseInt(inStr);
// the second line is the administration password
adminPassword = inFile.readLine();
clients = new Client[N];
StringTokenizer strAnalyzer = new StringTokenizer(inStr, ",");
for (int i = 0; i<N; i++)
{
inStr = inFile.readLine();
// fill the fields with the record
String firstName = strAnalyzer.nextToken();
String lastName = strAnalyzer.nextToken();
String ssn = strAnalyzer.nextToken();
String accNumber = strAnalyzer.nextToken();
byte accType = Byte.parseByte(strAnalyzer.nextToken());
byte accStatus = Byte.parseByte(strAnalyzer.nextToken());
float accAmount = Float.parseFloat(strAnalyzer.nextToken());
String pin = strAnalyzer.nextToken();
// create new Client object
clients[i]= new Client(firstName, lastName, ssn, accNumber,
accType, accStatus, accAmount, pin);
}
// close the input file
inFile.close();
return true;
}
thanks
crq
-
at BankDatabase.readFile(BankDatabase.java:26)
So... what's at line 26 of BankDatabase.java? This is the first thing you should be looking at in all runtime errors.
-
since you're instructor told you to use StringTokenizer you should stick with it, but I just thought i'd tell you that the StringTokenizer class is now deprecated. This means that it is only in the current library for backward compatibility, but Sun recommend not using it in any new code because it will likely be taken out of the library sometime in the future.
Sun reccommends using the youString.split(aParameterToSplitOn) method. This method will split your string up on the specified parameter, and return the bits of your string in an array.
Code:
String yourString = "a,b,c,d,e";
String[] stringArray = yourString.split(",");
This will produce an string array of length 5, and each index will contain one of the letters. The commas won't be in there at all because it removes them.
If you want a better tutorial go see Cjards signature, he has a link something he wrote on the split() method on there.
As to the question you asked, as Drain said you need to look at what is at line 26. The error message tells you exactly which line of your code the error was found at.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks