Results 1 to 5 of 5
Thread: help me :s
- 04-09-2010, 06:57 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 6
- Rep Power
- 0
help me :s
hi evrey one i have this sheet and i Wrote this program
The program contains no errors But the output is false
I want to know what mistakes in my program :confused:
can any one help me ? :(
Question
Write a program to read an email address and do the following:
1- Get the full name part from email addresses, assign it to a string (Fullname) in this format : Last, First
2- Get email domain and assign it to a string variable (Domain)
3- Display name and domain .
Note : email is entered in this format (first.last@domain)
Ex:
Input email : ali.alsaleh@ksu.edu.sa
you should output :
Full Name is : Alsaleh, Ali
Domain is : ksu.edu.sa
and this is my program :>
import java.util.*;
public class sheet6q2
{
public static Scanner console = new Scanner (System.in);
public static void main (String[]args)
{
String firstname,lastname,Domain , email ;
System.out.println("plz entar your email :");
email= console.next();
firstname = email.substring(0,'.');
lastname= email.substring('.','@');
Domain= email.substring('@',email.length() - 1);
System.out.println("full name is :"+" "+ lastname+","+firstname +" Domain is :"+ " "+ Domain);
}
}
- 04-09-2010, 07:11 PM #2
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
The problem is the substring method needs an int as a parameter...
try using the indexOf method in addition.
ex:
firstName = email.substring(0, email.indexOf('.'))
that should work
- 04-09-2010, 07:36 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 6
- Rep Power
- 0
- 04-09-2010, 07:42 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
Again, with the substring method, you can only use int's as parameters.
With:
you are passing in char's, if you are looking for char's, you need to use the indexOf method. you had the @ correct, so now try the below example, and see if you can figure out the last one on your own.Java Code:lastname= email.substring('.', email.indexOf('@'));
good luckJava Code:lastname= email.substring((email.indexOf('.') + 1), email.indexOf('@'));
-
Another option is to call String#split("\\.") using the period as the regex to split on:
Much luck!Java Code:String test = "foo.bar"; String[] tokens = test.split("\\."); System.out.println(Arrays.toString(tokens));


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks