954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Anyone mind helping out a stuck noob?

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() + ".");
 
}
}";
Teachingmyself
Junior Poster in Training
95 posts since Dec 2006
Reputation Points: 14
Solved Threads: 4
 

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

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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

Teachingmyself
Junior Poster in Training
95 posts since Dec 2006
Reputation Points: 14
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You