JAVA classes/constructors question

  • Context: Comp Sci 
  • Thread starter Thread starter csgirl504
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a question related to Java programming, specifically concerning how to extract individual words from a string argument passed to a class constructor. The context is a homework problem focused on understanding string manipulation within Java.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant inquires about extracting the first and second words from a string argument in a Java constructor.
  • Another participant suggests using the Java String class method called split() as a potential solution.
  • A later post indicates that the original poster has resolved their question independently.

Areas of Agreement / Disagreement

The discussion does not contain significant disagreement, as the original poster found a solution after receiving a suggestion.

Contextual Notes

None.

Who May Find This Useful

Individuals learning Java programming, particularly those interested in string manipulation and class constructors.

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! :)
 
Good! ;)
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K