Username String suggestion  
Author Message
Yuriy_Ivanov





PostPosted: 2005-12-15 4:55:00 Top

java-programmer, Username String suggestion I'm doing Java excercises in University. Part of a program I am working on
requires me to input a both a surname and forename together and have the
program output the forename name only.

e.g:
Please enter student's name: Yuriy Ivanov
Yuriy scored (+ score) which is a (+ grade)

The only way I can think of doing this is to use a String method to output
everything until it hits a space, does this sound probable?
Of course, I would need validation incase only a space is entered.

If this sounds like I'm on the right lines please can someone tell me how
to do that, if not please can someone tell me a better way. I've only know
of String concatination which is the opposite of what I need.

If there is a way to do this without methods I'd prefer to have that as
this part of the program isn't meant to include methods or arrays yet.

Thank you in advance.


 
klynn47





PostPosted: 2005-12-15 6:29:00 Top

java-programmer >> Username String suggestion If a space is what defines a seperation between the names, then you
could simply use the split method of String.

 
Monique Y. Mudama





PostPosted: 2005-12-15 7:02:00 Top

java-programmer >> Username String suggestion On 2005-12-14, Yuriy_Ivanov penned:
> I'm doing Java excercises in University. Part of a program I am
> working on requires me to input a both a surname and forename
> together and have the program output the forename name only.
>
> e.g: Please enter student's name: Yuriy Ivanov Yuriy scored (+
> score) which is a (+ grade)
>
> The only way I can think of doing this is to use a String method to
> output everything until it hits a space, does this sound probable?
> Of course, I would need validation incase only a space is entered.
>
> If this sounds like I'm on the right lines please can someone tell
> me how to do that, if not please can someone tell me a better way.
> I've only know of String concatination which is the opposite of
> what I need.
>
> If there is a way to do this without methods I'd prefer to have that
> as this part of the program isn't meant to include methods or arrays
> yet.
>

I don't quite know what you mean by "without methods". Even reading
the data from stdin requires method usage. Perhaps you mean without
implementing your own methods?

Spaces can be problematic, as some last names have spaces in them.
And what if the student enters a middle name as well?

But I'm guessing your instructor is assuming a single word each for
the first and last names.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
 
Roedy Green





PostPosted: 2005-12-15 7:19:00 Top

java-programmer >> Username String suggestion On Wed, 14 Dec 2005 15:54:30 -0500, "Yuriy_Ivanov"
<email***@***.com> wrote, quoted or indirectly quoted
someone who said :

>The only way I can think of doing this is to use a String method to output
>everything until it hits a space, does this sound probable?

to solve that problem commercially takes pages and pages of code. If
you have only two names, there in not a big problem but if the name is
something like Yuri St. Gluwein or Yuri van Gluwein you have the
problem of figuring ot what the middle thing is.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Yuriy_Ivanov





PostPosted: 2005-12-15 7:41:00 Top

java-programmer >> Username String suggestion I need to design it without the use of my own methods, that is what I
meant, at least until I develop the second part of the program to add onto
it.

I know there must be a simple solution they are looking for since we're
beginners in this course, so it should be assuming the user inputs single
words for the names.



 
 
Monique Y. Mudama





PostPosted: 2005-12-15 8:04:00 Top

java-programmer >> Username String suggestion On 2005-12-14, Yuriy_Ivanov penned:
> I need to design it without the use of my own methods, that is what
> I meant, at least until I develop the second part of the program to
> add onto it.
>
> I know there must be a simple solution they are looking for since
> we're beginners in this course, so it should be assuming the user
> inputs single words for the names.
>
>

Yup, I tend to agree. Although it can't hurt to ask your teacher if
you're supposed to handle those types of cases. S/he may be impressed
and more likely to help you in the future =)

PS: It is good newsgroup practice to "quote" the text to which you're
responding, as I've done with your text here. It makes it easier to
follow the conversation. (My newsreader happens to color-code
responses to my posts and bump them to the top of the list, so at
least I knew that this was a followup to my post.)

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
 
Hal Rosser





PostPosted: 2005-12-15 8:24:00 Top

java-programmer >> Username String suggestion
"Yuriy_Ivanov" <email***@***.com> wrote in message
news:email***@***.com...
> I'm doing Java excercises in University. Part of a program I am working on
> requires me to input a both a surname and forename together and have the
> program output the forename name only.
>
> e.g:
> Please enter student's name: Yuriy Ivanov
> Yuriy scored (+ score) which is a (+ grade)
>
> The only way I can think of doing this is to use a String method to output
> everything until it hits a space, does this sound probable?
> Of course, I would need validation incase only a space is entered.
>
> If this sounds like I'm on the right lines please can someone tell me how
> to do that, if not please can someone tell me a better way. I've only know
> of String concatination which is the opposite of what I need.
>
> If there is a way to do this without methods I'd prefer to have that as
> this part of the program isn't meant to include methods or arrays yet.
>
> Thank you in advance.
>

I suspect the instructor is looking for you to use some methods of the
String class.
Use the indexOf(String s) method to get the index of the space in the the
String, then use the subString method to get the first name - starting with
index 0 up to index of the space.