Anyone mind helping out a stuck noob?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 95
Reputation: Teachingmyself is an unknown quantity at this point 
Solved Threads: 4
Teachingmyself's Avatar
Teachingmyself Teachingmyself is offline Offline
Junior Poster in Training

Anyone mind helping out a stuck noob?

 
0
  #1
Dec 1st, 2006
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.

  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. }";
THEY MADE ME DO IT
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Anyone mind helping out a stuck noob?

 
0
  #2
Dec 1st, 2006
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);
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 95
Reputation: Teachingmyself is an unknown quantity at this point 
Solved Threads: 4
Teachingmyself's Avatar
Teachingmyself Teachingmyself is offline Offline
Junior Poster in Training

Re: Anyone mind helping out a stuck noob?

 
0
  #3
Dec 1st, 2006
That's great, thanks a million for the help!!! =)
THEY MADE ME DO IT
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC