• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays  RSS feed

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay I know there is some stupid mistake in here but I do not know what it is. When I run the following program all I get are nulls please tell me what I am forgeting
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well just taking a quick scan

private String mansName;
private String femaleName;
private String lastName;

Never have any values assigned to them.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's just take a simple example. Your Names class has an instance variable named mansName. When you create an instance of the class (new Names()), that variable is set to null. You don't have any code that assigns a value to that variable anywhere in the class. So when you call getMensFirstName() on a Names object, it's always null. That's why you get null, because you never assign anything else to it.

Now I suppose your code in the getMensFirstName() method is supposed to use the Scanner object to extract some data from that text file and assign it to the mansName variable? Is that right? If so, the "assign to mansName variable" part is what you are missing.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct I am reading the names for text files. I inialized the vairables and now my output is blank.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
If so, the "assign to mansName variable" part is what you are missing.


How do I do this?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Victoria Preston:


How do I do this?


 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay that works thanks....now I have a new problem....it keeps reading the same name from the file its suppose to read the first one and then the scond one and so on.....looking at my code cna you tell me whats wrong.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You open and close the file everytime you call the methods.

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am but it only reads the first name
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the problem - Hence I showed you the problem in your method.

If you close and reopen the file it will restart at the beginning of the file every time.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get what your saying. I even thought of that, but even if I take out the s.close(). I still get the same last name each time.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are still doing


[ September 18, 2006: Message edited by: Tim LeMaster ]
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, where should that line be then...I moved it out of the try. Am I suppose to remove it from the method all together
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should only open the file once and the read from multiple times. Moving it outside the try won't help. It needs to be in a different method then getXXXName() if you intend to call getXXXName() multiple times.

Time to hit the books.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim is right. You have two (or possibly more) choices, you can either read all into memory at once and store them in a List or an array and access them that way, or you can read and throw away all the lines in the list up to the nth name.



Or you could use a RandomAccessFile.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try...I have been doing research on this all weekend...and I have 4 books I am flipping through.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... forgot to increment my counter. That should read:

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now all I get is blank spaces
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your latest code so that we can see what changes you have made.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!