Results 1 to 5 of 5

Thread: help me :s

  1. #1
    MSs.Java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

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

    }
    }





  2. #2
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  3. #3
    MSs.Java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by mchammer11 View Post
    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
    thx alot :) I've done your advice But there are also errors in outputs :(


    firstname = email.substring(0, email.indexOf('.'));



    lastname= email.substring('.', email.indexOf('@'));



    Domain= email.substring('@', email.length()-1);


    :confused:

  4. #4
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    Again, with the substring method, you can only use int's as parameters.
    With:
    Java Code:
     lastname= email.substring('.', email.indexOf('@'));
    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('.') + 1), email.indexOf('@'));
    good luck

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Another option is to call String#split("\\.") using the period as the regex to split on:
    Java Code:
        String test = "foo.bar";
        String[] tokens = test.split("\\.");
        System.out.println(Arrays.toString(tokens));
    Much luck!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •