Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2003, 01:57 PM
digital-bullet digital-bullet is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 2 digital-bullet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to digital-bullet
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.

Reply With Quote
  #2  
Old August 7th, 2003, 02:13 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,357 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 7 h 10 m 45 sec
Reputation Power: 787
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.

Reply With Quote
  #3  
Old August 8th, 2003, 04:22 AM
ishnid's Avatar
ishnid ishnid is online now
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,573 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 10 h 49 m 31 sec
Reputation Power: 1411
Quote:
StringChange.java:26: incompatible types
found : char
required : java.lang.String
firstOne = sentence.charAt(0);


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;

Reply With Quote
  #4  
Old August 8th, 2003, 04:41 AM
digital-bullet digital-bullet is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 2 digital-bullet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to digital-bullet
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!");
}

}

Reply With Quote
  #5  
Old August 8th, 2003, 05:30 AM
ishnid's Avatar
ishnid ishnid is online now
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,573 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 10 h 49 m 31 sec
Reputation Power: 1411
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;

Reply With Quote
  #6  
Old August 8th, 2003, 12:47 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,357 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 7 h 10 m 45 sec
Reputation Power: 787
Whew! A quick lesson in Java. That looks easy enough. I might have to learn that soon. Thanks for the good explanations ishnid.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Noob needs help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway