Results 1 to 3 of 3
- 08-12-2011, 05:54 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
stuck on this pls help(validation and char.toString)
we were asked by our professor to do a simple class wherein we need to make it ask the user to enter the first name middle name and last name so i came up with the class like this:
the output is always like this:Java Code:import java.io.*; public class StringManipulation { public static void main(String args []) throws IOException { BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("Enter First Name: "); String k = dataIn.readLine(); System.out.print("Enter Middle Name: "); String j = dataIn.readLine(); String m = Character.toString(); System.out.print("Enter Last Name: "); String s = dataIn.readLine(); System.out.println(k + " " + j + " " + s); }catch(Exception m) { m.printStackTrace(); } } }
but then after that when i showed it to her she wants me to make the final output to be like this:Enter First Name: John
Enter Middle Name: Jhay
Enter Last Name: Smith
Your Name Is: John Jhay Smith
how can i make it like that? i used char.toString code but i always come up with an error and no matter what i do i can't seem to fix it.Enter First Name: John
Enter Middle Name: Jhay
Enter Last Name: Smith
Your Name Is: John J. Smith
also she wants me to make a validation code for it where in the user can only input alphabets and the special characters "." and "-" i tried using if(!Character.isDigits()) but still gives out an error .. can anyone help?
- 08-12-2011, 07:14 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
String m = Character.toString(); <-- what ?
You could use charAt(0) to get the first Character, thats what you want or?
char m =j.charAt(0);
....
System.out.println(k + " " + m + ". " + s);
For the other one you could use regex,something like if(k.matches("[a-zA-Z\\.-]+")).....
- 08-12-2011, 07:39 AM #3
The toString method will convert a char to a String which is not what you want. Even if it was, which char is it supposed to convert to a String? Did you look at the API to see what parameters that method has?Java Code:Character.toString();
Once again did you read the API to see if the Character class has that method. You cannot just make up method names.Java Code:if(!Character.isDigits())
In future copy and paste the full and exact error message so we can see it. We don't read minds.but still gives out an error
Similar Threads
-
concatenate two char into one char
By nadissen in forum EclipseReplies: 3Last Post: 04-06-2011, 02:40 PM -
searching char array with another char array for full word matches
By karunabdc in forum New To JavaReplies: 2Last Post: 03-08-2011, 06:20 AM -
check a string char by char
By Sotsiak in forum New To JavaReplies: 2Last Post: 10-23-2010, 09:24 PM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 05:48 AM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks