Results 1 to 10 of 10
  1. #1
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default 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:
    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();
    so at the moment i have:
    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
    unfortunately it doesn't like String n = fin.next();
    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
    Last edited by hiei_yasha; 02-20-2011 at 03:39 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hiei_yasha View Post
    so at the moment i have:
    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
    unfortunately it doesn't like String n = fin.next();

    Are you 100% sure about this? Are you sure that this is the line causing the error?

    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
    Scanner#next() returns a String, so this shouldn't be a problem. The bigger problem I see is here:

    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?

  3. #3
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Are you 100% sure about this? Are you sure that this is the line causing the error?
    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 :/

    Quote Originally Posted by Fubarable View Post
    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?
    so do i need to pass in.. Address parameters?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hiei_yasha View Post
    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 :/
    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.


    so do i need to pass in.. Address parameters?
    I don't know. You have access to the Address class code and we don't.

  5. #5
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hiei_yasha View Post
    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.
    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.

    sorry to go off topic. got the thing to work
    And this is what really matters. Congrats.

  7. #7
    hiei_yasha is offline Member
    Join Date
    Jan 2011
    Posts
    34
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    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!

  10. #10
    Clerek is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 09:29 AM
  2. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 12:43 PM
  3. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 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
  •