944,103 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1249
  • Java RSS
Dec 1st, 2006
0

Anyone mind helping out a stuck noob?

Expand Post »
Hey whoever was good enough to take a look at this thread I have to stress I am a complete noob!


I picked up "Java programming for the complete beginner" five days ago and here I am with 50% less hair and stuck on a challenge at the end of chapter 2!

My problem is the sparse table in the book re String class methods.All I have is a page with 17 methods and a brief description,no worked examples etc.

Here is the code I've been trying to compile for two days,in its latest re-jig.

php Syntax (Toggle Plain Text)
  1. echo"
  2. /*Letters.java*/
  3. /*I'm trying to create a program that capitalises the first letter of the user-defined input
  4. and parses the rest to lower case then adds a "." at the end(Java Programming for complete
  5. beginners pg 69 challenge 4)*/
  6.  
  7. import java.io.*;
  8.  
  9. public class Letters {
  10.  
  11. public static void main(String args[]) {
  12.  
  13.  
  14. String value = "",
  15. carA = "",
  16. restA = "";//strings loaded
  17.  
  18.  
  19.  
  20. BufferedReader reader;
  21. reader = new BufferedReader(new InputStreamReader(System.in)); //the usual
  22.  
  23. try {
  24. System.out.println("Please type a word :\t");
  25. value = reader.readLine();
  26.  
  27.  
  28. }
  29. catch(IOException ioe) {
  30. System.out.println("IO ERROR");
  31.  
  32. }
  33.  
  34. carA = value;
  35. value = value.charAt(0);//these lines are my problem according to javac
  36. restA = carA.substring(1);//"found char, required java.lang.String"
  37.  
  38.  
  39. System.out.println(carA.toUpperCase() + restA.toLowerCase() + ".");
  40.  
  41. }
  42. }";
Similar Threads
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
Teachingmyself is offline Offline
95 posts
since Dec 2006
Dec 1st, 2006
0

Re: Anyone mind helping out a stuck noob?

The charAt method returns a character and you're trying to assign it to a String variable. Fixing that should prevent the second error.
You can turn a character into a string by adding a blank string with it.

String s = ""+value.charAt(0);

Your output still won't look right since you're outputting carA and restA.

carA equals whatever the user inputted, its not changed anywhere. So the first part written to the screen will be the entire input string capitalized.

restA equals carA, the same thing minus the first character. When you pass only 1 argument when calling substring, it returns the rest of the string from that character position and on to the end.


For example:

String carA = "something";
restA = carA.substring(1);

restA will equal "omething".

You're using 'value' to grab the first character but never actually doing anything with it.


Here's an example:
String input = "this is a test";

String fixed = input.substring(0,1).toUpperCase() + input.substring(1).toLowerCase() + ".";

System.out.println(fixed);
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Dec 1st, 2006
0

Re: Anyone mind helping out a stuck noob?

That's great, thanks a million for the help!!! =)
Reputation Points: 14
Solved Threads: 4
Junior Poster in Training
Teachingmyself is offline Offline
95 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: SOAP call constructor each time
Next Thread in Java Forum Timeline: hi.. string compare





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC