|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Noob needs help
Hi to whoever is reading this!
I have finally decided to learn Java. I am at the end of chapter 2 in a book and am having probs with a particular prog. Now, I dont want anyone to do it for me but I'm just wondering if someone could explain the error msg i keep getting? ----------------------------------------------------- StringChange.java:26: incompatible types found : char required : java.lang.String firstOne = sentence.charAt(0); ^ ----------------------------------------------------- The prog is a simple prog and is supposed to do the following. -------------------------------------------------- Reads in a sentence of mixed case and changes first letter to upper case and the rest lower case and puts a full stop on the end. ------------------------------------------------- I have managed to get half of it working i.e. it converts the entire sentence to lowercase but i'm having trouble identifying the first character. Any help will be much apreciated. |
|
#2
|
||||
|
||||
|
Welcome to the boards. Please read "How to post a question!" (link in sig) before posting any more threads. Your subject isn't very descriptive. Changing it will increase this threads visibility and increase your chances of getting the help you need. I'd help, but I don't do Java.
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#3
|
||||
|
||||
|
Quote:
the charAt() method returns the the character found at the position you specified. It is in the form of a 'char' primitive, rather than a String object. firstOne is a String, not a char, therefore you can't assign a char to it. I know it sounds strange seeing as a String is essentially just an array of chars. You'll have to expressly convert the char to a String: Code:
firstOne = String.valueOf(sentence.charAt(0)); valueOf() will convert a char into a String for you and you should avoid the error. ~ishnid;
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#4
|
|||
|
|||
|
thank you very much for your help. it is much appreciated.
The only thing i dont understand is that i wrote a prog before this one and used the exact same method and it worked fine. the source code is listed below. is there any chance you can tell me why it worked last time and not this time? thanks. /* * NameGame * N Sheikh * Demonstrates simple I/O and String Methods */ import java.io.*; public class NameGame { public static void main(String args[]) { String firstName = ""; String lastName = ""; String fullName; String initials; int numLetters; BufferedReader reader; reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("What is your first name? "); try { firstName = reader.readLine(); } catch (IOException ioe) { System.out.println("I/O Exception occurred"); } System.out.println("That's a nice name, " + firstName + "!"); System.out.println("I'll shout it! " + firstName.toUpperCase() + "!"); System.out.print("OK, what's your last name? "); try { lastName = reader.readLine(); } catch (IOException ioe) { System.out.println("I/O Exception Occurred"); } fullName = firstName; //alternative to using the '+' operator fullName = fullName.concat(" ").concat(lastName); System.out.println("Oh, so your full name is " + fullName + "."); System.out.println("Or sometimes listed " + lastName + ", " + firstName + "."); initials = firstName.charAt(0) + "." + lastName.charAt(0) + "."; System.out.println("Your initials are " + initials); numLetters = firstName.length() + lastName.length(); System.out.println("Did you know there are " + numLetters + " letters in your name?"); System.out.println("Bye!"); } } |
|
#5
|
||||
|
||||
|
First off, please use the [ code] tag when posting code as it preserves indentation and makes it all easier to read.
I assume you're referring to this line: Code:
initials = firstName.charAt(0) + "." + lastName.charAt(0) + "."; Unlike the first example, where you only have one char variable, here you're stringing them together using '+', creating a string, rather than a single character. ~ishnid; |
|
#6
|
||||
|
||||
|
Whew! A quick lesson in Java. That looks easy enough. I might have to learn that soon. Thanks for the good explanations ishnid.
|
![]() |
| Viewing: Dev Shed Forums > Other > Beginner Programming > Noob needs help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|