| | |
Anyone mind helping out a stuck noob?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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)
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() + "."); } }";
THEY MADE ME DO IT
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
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);
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);
![]() |
Similar Threads
- Searching a file for a string (C++)
- error C2447: missing function header (old-style formal list?), what does this mean?! (C++)
- rpg phping, fighting sparring please look (PHP)
- Beginner Networking (Networking Hardware Configuration)
- Translate an algorithm to C program (C)
Other Threads in the Java Forum
- Previous Thread: SOAP call constructor each time
- Next Thread: hi.. string compare
| Thread Tools | Search this Thread |
actionlistener android api applet application array arrays automation binary block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp draw eclipse error event exception fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie number online oracle print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update windows working





