Results 1 to 10 of 10
- 02-20-2011, 03:34 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
filling an array of Address objects..?
up until now we've just been filling arrays with strings and ints. apparently now we are supposed to fill them with objects..
scenario:
i have an input file consisting of a street (string), city (string), state (string), and zip (int).
we have a constructed Address.java file with default and explicit value constructor, a toString, compareTo, and equals methods.
we have a FileUtil class that opens the input file and fills the array.
at the moment i'm having trouble filling the array given this code in our main file:
so at the moment i have:Java Code:Address [] addresses = null; Scanner fin = new Scanner(new File("addresses.txt")); total = FileUtil.count(fin); fin.close(); fin = new Scanner(new File("addresses.txt")); addresses = FileUtil.fillArray(fin, total); fin.close();
unfortunately it doesn't like String n = fin.next();Java Code:public static Address [] fillArray(Scanner fin, int total){ String n = fin.next(); Address [] temp = new Address[total]; for (int i = 0; i <total; i++) { temp[i] = n; } return temp; }//end fillArray
at compile i get:
found : java.lang.String
required: Address
soo i've tried casting to an Address, Address n = fin.next(), but am stuck. any help appreciated as alwaysLast edited by hiei_yasha; 02-20-2011 at 03:39 AM.
-
Are you 100% sure about this? Are you sure that this is the line causing the error?
Scanner#next() returns a String, so this shouldn't be a problem. The bigger problem I see is here:at compile i get:
found : java.lang.String
required: Address
soo i've tried casting to an Address, Address n = fin.next(), but am stuck. any help appreciated as always
Java Code:temp[i] = n;
Where you try to assign a String object into an Address variable. How is Address constructed? Can an Address object be created via a constructor passing in a String parameter?
- 02-20-2011, 05:06 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
well, no, but at this stage i can only go by where the little pointer is at when i go to compile. it points at the space between the "t" in next and the "(" so i felt it was natural to think that's where the error was. i post in the new to java section for a reason :/
so do i need to pass in.. Address parameters?
-
I know you're posting to new for a reason and I'm not trying to put you down, but your location of the error doesn't make sense to me. Scanner#next() returns a String, so the error you state should not occur on the line you're indicating. I'm asking you to double check, if you you rebuild and recompile is the same error occurring on that specific line? If you can't accept our questioning your statements, you may not wish to be posting here.
I don't know. You have access to the Address class code and we don't.so do i need to pass in.. Address parameters?
- 02-20-2011, 05:30 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
well i do really respect you for helping me out, but to be honest with you bro the majority of the posts of yours i happen to see are basically answer a question with a question or are pretty over-the-top for the level of proficiency of the OP. i mean you can see it in their code, english/grammar, and whatnot.
you're probably pretty pro at this, but obviously not everyone's gonna be at your level.
for example if you knew nothing about tennis and you asked me what the best grip style was, i'm not gonna talk about "semi-western", "western", "eastern", and "continental". it probably won't mean much to you because you're just starting out. i'm gonna say "it's best to put this finger here your palm here and those fingers over there, adjust to your comfort level" or something along those lines.
sorry to go off topic. got the thing to work
Java Code:public static Address [] fillArray(Scanner fin, int total) { String street = fin.nextLine(); String city = fin.nextLine(); String state = fin.nextLine(); int zip = Integer.parseInt(fin.nextLine()); Address add = new Address (street, city, state, zip); Address [] temp = new Address[total]; for (int i = 0; i <temp.length; i++) { temp[i] = add; } return temp; }//end fillArray
-
We have to understand the problem before we can give advice. Again, your error made no sense at its location and asking for clarification was and is appropriate and in fact necessary if you still needed help and we were going to attempt to help you. I saw nothing wrong in doing that and still see absolutely nothing wrong with it.
And this is what really matters. Congrats.sorry to go off topic. got the thing to work
- 02-20-2011, 05:51 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 34
- Rep Power
- 0
i'm sorry, when you said
"Are you 100% sure about this? Are you sure that this is the line causing the error?"
i thought you were trying to be sarcastic.
i guess as they say tone doesn't come across via text unless of course you add tags! "[sarcasm][/sarcasm] " :)
at this point "gettin it done" is all i can go for. our teacher has given us 5 separate assignments from the past week, one was due last wednesday, one on friday, one today, one tomorrow, and one on tuesday. homework for my discrete math class due on tuesday, java test thursday, and math test friday. the java hw that was due last night i had spent roughly 30-35 hours on.
-
Best of luck with that course load! Remember, one step at a time...
- 02-20-2011, 11:23 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
this doesn't fill the array though. I'm doing a similar assignment (same class?) and all that does is fill the array with the object, repeatedly for the length of that array...can't figure this one out either!
- 02-20-2011, 11:25 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02-02-2011, 09:29 AM -
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11-12-2008, 12:43 PM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks