Jump to content

Multiple Strings passed in Constructor?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
I'm looking at passing multiple Strings through the Constructor, among other variables. So far I have

public Car(String make, String model, int year, String previousOwner)

{

       setMake(make);

       setModel(model);

       setYear(year);

       setPreviousOwner(previousOwner);

}


I want to be able to have multiple previousOwners able to be added. I was thinking of using an ArrayList, but not sure how to implement this.
Thanks in advance

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
public Car(String make, String model, int year,  ArrayList<String> previousOwnerList)

{

       setMake(make);

       setModel(model);

       setYear(year);

       setPreviousOwners(previousOwner);

}


//... somewhere in the middle of nowhere


public static void main(String[] args) {

    ArrayList<String> somePreviousOwners = new ArrayList<String>();

    somePreviousOwners.add( new String("Bill Gates") );

    somePreviousOwners.add( ... );

    ...

    

    Car car = new Car( "Ford", "F150", 2009, somePreviousOwners );

}



#3
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Got it working, Thanks :)
I'm now trying to implement this using .txt files. So far I have the make, model etc working. But not the previousOwners with the ArrayList

My text file reads:

"Ford" "Falcon" 2004 "Frodo Baggins", "Sam Gamgee"
"Holden" "Astra" 2008 "Bender", "Fry", "Leela"

Code wise to implement I have

try 

		{   while(input.hasNext() )// input the values from the file

                    {  

                          

                            myCar.setMake( input.nextLine() ); 

                            myCar.setModel( input.nextLine());

                            myCar.setYear(input.nextInt());

                            //code to implement arraylist of owners

                           


I've tried
myCar.setPreviousOwners(input.nextLine());
but this gives me one string of everyone, not seperate owners.

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
the nextLine() method grabs the entire line of text. You need to use a method that grabs the individual words instead.
I'm not sure what input reader you're using but with scanner you can use the .next() method.

If you're using a BufferedReader then you can call nextLine(), then split that string into smaller peices(strings).
Example:

data inside of text file:

Ford Falcon 2004 (Frodo Baggins, Sam Gamgee)


Scanner reader ...

while( readerHasMoreToRead )

    String line = reader.nextLine();

    Scanner anotherReader = new Scanner( line );

    String make = anotherReader.next(); // stores ford

    String model = anotherReader.next(); // stores falcon

    int year = anotherReader.nextInt(); // stores num

    String previousOwners = line.substring( line.indexOf('(') + 1, line.indexOf(')') ); // stores Frodo Baggins, Sam Gamgee

    ArrayList<String> ownersList ...

    for( String owners : previousOwners.split(",") ) // splits and parses the "previousOwners" string and stores into an arraylist

        ownersList.add(owners);


Another point this code makes is that you need to "identify" where your owners are in the text file. I have done this by using parentheses. You can use anything.

#5
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
This is what I'm after!
Thanks for your help lethalWire

I'll upload finished code soon for anyone else interested :)

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
btw this also works:

public car(String make, String model, int year, String ... previousOwner){

So you can add no matter how much Strings. Accessing them is like accesing an array - previousOwner[0] and so on..

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Sinipull said:

btw this also works:

public car(String make, String model, int year, String ... previousOwner){

So you can add no matter how much Strings. Accessing them is like accesing an array - previousOwner[0] and so on..

You'd still have to save all of your "previous owners" in an array or ArrayList when you're parsing the data so the constructor you provided wouldn't be useful.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users