Constructor with a string paramater?

  • Thread starter kirkulator
  • Start date
  • Tags
    String
In summary, to initialize a SoccerPlayerStats object using a formatted string, you can split the string on the colon characters, extract the relevant information from each element, and assign them to the appropriate attributes.
  • #1
kirkulator
33
0
<indent>
I have a class for soccer players with these attributes:
//players first name
private firstName;
//players lastName
private lastName;
//number of points scored by player
private int pointsScored = 0;
//number of assists the player had
private int assists = 0;
//the percentage kick penalty rate the player had
private double penaltyKickRate = 0.0;

i am assigned to have a constructor with a single formatted string parameter like this:

public SoccerPlayerStats(String playerinfo) {
//initialize values
}

where the playerinfo string is formatted this way
"Jane Doe: Points Scored: 3 Assists: 5 Kick Rate Percentage: 3"

how in the world can i initialize these values in the contructor? With substrings maybe? It just seems a little wacky to me. Any tips will help : )

- Amanda
</indent>
 
Physics news on Phys.org
  • #2
Yes, you can use substrings to parse the information from the formatted string. First, you'll want to split the string on the colon characters and then create substrings from each segment of the split string. Then you can parse the values from each substring and assign them to the appropriate attributes. For example, you could use the `String.split` method to split the string on the colon characters: String[] playerInfoArray = playerInfo.split(":");Then you can extract the relevant information from each element in the array. For example: this.firstName = playerInfoArray[0].trim();this.lastName = playerInfoArray[1].trim();this.pointsScored = Integer.parseInt(playerInfoArray[2].trim());this.assists = Integer.parseInt(playerInfoArray[3].trim());this.penaltyKickRate = Double.parseDouble(playerInfoArray[4].trim());Hope this helps!
 

1. What is a constructor with a string parameter?

A constructor with a string parameter is a special method within a class that is used to initialize an object with a string value. It allows the user to pass a string as an argument when creating an instance of the class.

2. Why is a string parameter used in a constructor?

A string parameter in a constructor is used to provide initial values for the object's properties. It allows the user to customize the object's properties at the time of its creation.

3. How is a string parameter declared in a constructor?

A string parameter is declared in a constructor by including a string variable in the parentheses after the constructor's name. For example, if the constructor's name is "Person", the string parameter could be declared as "Person(String name)".

4. Can a constructor with a string parameter be overloaded?

Yes, a constructor with a string parameter can be overloaded. This means that multiple constructors can be created with different string parameters, allowing for more flexibility in creating instances of the class.

5. What are the benefits of using a constructor with a string parameter?

Using a constructor with a string parameter allows for more customization and flexibility in creating objects. It also helps to ensure that the object is properly initialized with the correct values. It can also make the code more readable and maintainable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top