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!:sad:

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.

echo"
/*Letters.java*/
/*I'm trying to create a program that capitalises the first letter of the user-defined input 
and parses the rest to lower case then adds a "." at the end(Java Programming for complete
beginners pg 69 challenge 4)*/
 
import java.io.*;
 
public class Letters {
 
public static void main(String args[]) {
 
 
 String  value = "",
  carA = "",
  restA = "";//strings loaded
 
 
 
 BufferedReader reader;
 reader = new BufferedReader(new InputStreamReader(System.in)); //the usual
 
 try {
 System.out.println("Please type a word :\t");
 value = reader.readLine();
 
 
}
 catch(IOException ioe) {
 System.out.println("IO ERROR");
 
}
 
 carA = value;
 value = value.charAt(0);//these lines are my problem according to javac 
 restA = carA.substring(1);//"found char, required java.lang.String"
 
 
 System.out.println(carA.toUpperCase() + restA.toLowerCase() + ".");
 
}
}";

Recommended Answers

All 2 Replies

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);

That's great, thanks a million for the help!!! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.