• 0

[JAVA] String Functions


Question

I'm stuck on the last part of my Java assignment, and unfortunately I can't remember how to do it. Its just a string function, I need to take a string and convert the first character to uppercase. If anyone can give me a quick answer it would be great.

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

One method would be to use a stringbuffer and return the first character into a new string. Use the toUpper method of the string class on the new string. Then do a replace on the stringbuffer and turn it back into a string.

I hope this didn't sound too confusing. Use the api to follow my sequence of steps and you will realize it's very easy.

Link to comment
Share on other sites

  • 0

Well I remember how to use a StringBuffer, and that would probably be easier. However this stupid introductory Java course I'm taking isn't far enough in to use that. How would I do this with just String functions?

Link to comment
Share on other sites

  • 0

You could use the substring method and create 2 strings out of the original. Then use toUpper on the string that contains the first letter and then append the two strings together. You'll get your original back with the first letter capitalized.

Link to comment
Share on other sites

  • 0

Well I don't know much about the substring method, but don't I need to know the exact length of the String in order to do it? For instance:

String word = "hello";

String temp1 = word.substring(0).toUpperCase();

String temp2 = word.substring(1,4).toLowerCase();

String word = temp1 + temp2;

But since I'm submitting the program to the teacher, I have no idea what he is going to input to make sure that it works.

Link to comment
Share on other sites

  • 0

substring(0,1)

then on the second round

substring(1, string.length())

Try this. Run through it with various strings. If all you are doing is capitalizing the first letter of the string this should work. You also don't need to lowercase the second string, correct? If I understand your first question, you just want to upper the first letter of the string, so you don't need to lower anything because you might have capitals in the rest of the string that you want to keep. Anyways, try it out.

Link to comment
Share on other sites

  • 0

No problem. Again, using the stringbuffer is probably the way that you would want to do it later on. Strings aren't meant to be changed. Stringbuffers are. Then again, what do I know. I enjoy helping because it improves my skills and I like helping others, as long as I feel they are learning something.

Link to comment
Share on other sites

  • 0
No problem.  Again, using the stringbuffer is probably the way that you would want to do it later on.  Strings aren't meant to be changed.  Stringbuffers are.  Then again, what do I know.  I enjoy helping because it improves my skills and I like helping others, as long as I feel they are learning something.

584730072[/snapback]

Well I learned a few months ago about how to use a StringBuffer, and I actually found my assignment that used it to do the same thing. I'm sure glad I'm paying $10,000 a year to take a Java course that is less advanced than my high school one.

Link to comment
Share on other sites

  • 0

Well, get used to it. The more advanced you are in high school, the less you're likely to learn in the beginning college classes. It will get more involved, well hopefully. Depends on where you go. I learned most of my java at my job. A matter of fact, I never used swing until I was an intern. Now I'm really good with it. I've done way more at work than I ever did in school, not counting a couple of exceptions.

Link to comment
Share on other sites

  • 0

Well it gets better next year, we'll be getting into some C++. I'm also going to keep taking the Java courses, I have a feeling that its going to be around for a few years, so what the hell, I may as well know it.

Link to comment
Share on other sites

  • 0

C++ and Java will be around for awhile. Java is huge where I work, and I work for the military, so we are not talking about a small company. A matter of fact, in my specific area, you can forget about getting a job if your gpa isn't above 3 and you don't know java. We've had c++ apps go to java recently.

Another one to think about is C#. It isn't used that much where I am, but I'm willing to bet it's going to be here to stay for awhile. Actually, I kinda like C#.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.