JAVA classes/constructors question

  • Context: Comp Sci 
  • Thread starter Thread starter csgirl504
  • Start date Start date
  • Tags Tags
    Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
csgirl504
Messages
18
Reaction score
0

Homework Statement



I just have a quick question and hope I can ask it clearly. If I am writing a constructor for a class that takes in one value as an argument, but the value is two words, how can I extract the first and second word?

For example, if I wrote a class called Name and my constructor looked like this:

Name(String firstLast)

And the value sent in was John Smith

How would I extract John and store it in its own variable? Is this even possible?


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
csgirl504 said:

Homework Statement



I just have a quick question and hope I can ask it clearly. If I am writing a constructor for a class that takes in one value as an argument, but the value is two words, how can I extract the first and second word?

For example, if I wrote a class called Name and my constructor looked like this:

Name(String firstLast)

And the value sent in was John Smith

How would I extract John and store it in its own variable? Is this even possible?

Hi csgirl504! :smile:

The java String class has a method called split() that can do that.
 
I figured it out! Thanks! :)