• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with some code  RSS feed

 
Jon Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to Java and I'm currently using a site called Programmr to practice. Anyways there is a problem that I had tried to solve for 4 hours today and I can't seem to nail it down. I know I am close but any help would be great!

"A String variable, fullName, contains a name in the following format:
first name last name (single blank)

Extract the first name into the String variable firstName and the last name into the String variable lastName.
Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables. "



 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things...

I think you are starting off by making this too hard. For example:

"Assume the variables have been declared and fullName already initialized."
So why are you creating a scanner to read the full name in? The above directions to me mean you can simply do this:

you don't need to worry about HOW it got that way, so you can get it that way however you want.

So now you have fullName populated. How would you split it? Generally, people here would tell you to StopCoding (<---that's a link. Click it). The main thing a programmer does is NOT write code, but instead THINKS about the problem. Solve it with paper and pencils. Literally write out the directions you would hand someone else if you wanted them to do it.

Writing a single line of code before you know what you are going to write is a BAD idea.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post. Always use them: doesn't it look better
I also deleted some excess blank lines. You hardly ever need more than one blank line at a time.
Assuming you have changed line 7 to read
String fullName = "Jon Johnston";
…how far have you got with it?
 
Jon Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the code you see was already written as part of the problem. I can't just put a name in because when running the code it has to be able to split whatever name you put in.
And thank you, yes it looks much better! =D
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,

Thank you for clarifying the code you posted is the template for the question.

Now, as Fred and Campbell asked, how do you think you will solve it? If I tell you my name is "Chris Barrett", using words (or, better yet, pseudocode) explain the mental steps that you (not a computer program, but you as a person) use to figure out what is my first name and what is my last name. You cannot write code to solve a problem until you know how to solve the problem (boy, that sounded very Zen like...).

If you are thinking we will just tell you, that's really not how coderanch works. We want you to learn how to do it yourself.

Cheers!
Chris
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Johnston wrote:I know I am close but any help would be great!

so then how do you know this? If the above code was handed to you as-is, then how close have you gotten? What have you written to accomplish this task?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, how far have you got? You need to delete the } on line 22 otherwise you have too many }s. Then you can run it and get this sort of output:-
java namessplit
Enter the full name:
Campbell Ritchie
The names after split is:
'
Remembering that Scanner#nextLine returns a String, go through the [link→]String documentation and look for methods which might help you.

Note: there are at least two good ways to achieve this result.

Advanced challenge: When you have got it to work, have a look at this new class and see if you can't use it to display names with any number of middle names.
 
Jon Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Sorry for my delayed response.

Well I was close to figuring it out but I lost the file I was working on and so I'm back to square one. When I was originally working on it I had a For loop but the output was printing stuff out vertically and I don't think the problem was meant to have a For loop used. And while normally I would be fine using any method to solve the problem, I want to solve the problem using a more basic string method since I am following an online course, hopefully that makes sense =).

And I know in my mind I am making this more complicated than it needs to be but I find myself getting frustrated cause I can't figure it out lol. Ok so I'm going to trying and talk about how I would solve this (and I really liked "Not a Code Mill" thing).

Well first I need to have some sort of .split method and I'm pretty sure " fullName.spilt(""); " should work. But its the second part I really can't figure out, how to get the first name and last name to be printed out. I checked out the String Doc from oracle but I'm not sure which method to use to achieve this. Do I use a .substring or .length? And I tried both but with no luck.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the split() method do? What do you pass to it, and what does it return? look at the java.lang.String class API if you don't know.

you should never say "i think this should work" when programming. You should say "I KNOW this will work because". You may be wrong in the end, but guessing is never going to let you get very far.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware: if you look at String#split in the documentation, they give a particular name to the parameter. It is called regex. That means it is a String representing a regular expression. Regular expressions are a whole science to themselves, but you can find about them in the Java Tutorials.
What happened when you tried splitting with ""? Try this, where the class to import is java.util.Arrays:-
System.out.println(Arrays.toString("Jon Johnston".split(""));
With a little jiggery‑pokery with that sort of line, you can change "" to a String regex read from the keyboard and try different arguments to find what results you get.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!