Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree6Likes

Thread: Anybody please help?? :(

  1. #1
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default Anybody please help?? :(

    I want to write a java code to extract phone number from a given text file which contain other data like name, address and other details. i only need to extract the phone no: from such a file. anybody please help me to do that... as soon as possible.
    i am new to java.

    i have gone through the program How do you read from a file, and then store the info in an array?

    but my text file contain character along with phone no:
    and i think that slight modifications in this program will do.. i'm not sure.. there might be some other simple ways

  2. #2
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    anybody please help....

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    What is the textual format of a phone number?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    What is the format of the text file? It might help if you posted a few sample lines from the file into your response, surrounded by "CODE" markup. (Use the '#' icon above the text box.)

  5. #5
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Cool

    Yes it is simple... or maybe the logic is simple for me. This is what I made for an old Java Program I used to get information about doctors from a text file.
    Java Code:
    public void collectInfo(String path) {
    //This will hold the phone's value after the fact.
    String phone;
    try {
    	//Create InputStream Object
    	InputStream in = DoctorProfileApp.class.getResourceAsStream(path);
    	//Create BufferedReader Object
    	BufferedReader buf = new BufferedReader(new InputStreamReader(in));
    	//Create string to hold text that comes from BufferedReader
    	String text;
    	//Read each line
    	while((text = buf.readLine()) != null) {
    		//int findPhone tells you where the line that has "PhoneNumber=" starts
    		int findPhone = text.indexOf("PhoneNumber=");
    		//If this line exists
    		if(findPhone != -1 ) {
    			//Remove "PhoneNumber=" from the line, leaving you with just the actual number
    			text = text.replace("PhoneNumber=", "");
    			//Print to standard out
    			System.out.println(text);
    			//Give text's value to the string phone in case you need it
    			phone = text;
    		}
    	}
    	//Close everything!!!
    	buf.close();
    	in.close();
    }catch(IOException e) {System.err.println(e);} 
    	catch(NullPointerException e) { System.err.println("That file did not load"); }
    }
    This should work but tell me if there are any compiler errors.
    durga.laxmi likes this.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  6. #6
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Yes sir, i think i'm near to the solution
    thanks alot sir..

    Code:-

    Java Code:
    import java.io.*;
    
    public class docph {
        public static void main(String[] args) {
            String[] lines = new String[0];
            String path = "input.txt";
            BufferedReader br = null;
            String phone;
            try {
                File file = new File(path);
                br = new BufferedReader(
                     new InputStreamReader(
                     new FileInputStream(file)));
                String text;
                while( (text = br.readLine()) != null ) { 
    int findPhone = text.indexOf("PhoneNumber=");
    if(findPhone != -1 ) {
    //Remove "PhoneNumber=" from the line, leaving you with just the actual number
    text = text.replace("PhoneNumber=", "");
    //Print to standard out
    System.out.println(text);
    //Give text's value to the string phone in case you need it
    phone = text;
    		}
    	}
    	//Close everything!!!
    	br.close();
    	//in.close();
    }catch(IOException e) {System.err.println(e);} 
    catch(NullPointerException e) 
    { System.err.println("That file did not load"); }
    }
    }
    My text file contained the following details (File name input.txt)

    Java Code:
    Toyota PhoneNumber= 1456328795
    Subaru PhoneNumber= 6954013010
    Volkswagen  PhoneNumber= 5432869823
    Chevrolet
    Ford
    Chrysler
    Dodge
    and the output was:-
    Toyota 1456328795
    Subaru 6954013010
    Volkswagen 5432869823

    but now what i need is that i don't want that names along with the number, what can i do to remove that names, i just need the phone numbers only...

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Use indexOf to find the location of the = and then use substring if it is greater the 0.
    JeffGrigg and durga.laxmi like this.

  8. #8
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I suggest learning the "format code" function of your IDE of choice.

    And I would do something like this:
    Java Code:
    				String phoneNumberText = "PhoneNumber=";
    				int findPhone = text.indexOf(phoneNumberText);
    				if (findPhone != -1) {
    					// Remove "PhoneNumber=" from the line, leaving you with
    					// just the actual number
    					phone = text.substring(findPhone + phoneNumberText.length() + 1);
    					// Print to standard out
    					System.out.println(phone);
    				}
    durga.laxmi likes this.

  9. #9
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    substring if greater than 0 means, i dint get u sir. Sorry
    what should i give as parameters for substring()

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

    Default

    Quote Originally Posted by durga.laxmi View Post
    substring if greater than 0 means, i dint get u sir. Sorry
    what should i give as parameters for substring()
    Have you read the String API? It's all spelled out for you there.
    durga.laxmi likes this.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by durga.laxmi View Post
    substring if greater than 0 means, i dint get u sir. Sorry
    what should i give as parameters for substring()
    Would you like us to spoonfeed you the code so you don't have to learn anything?

  12. #12
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    yes ofcourse sir, i have heard of it . bt am not dat much familiar to use it. thatsy!

    i got the output when i changed my code as per you said

  13. #13
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Oh no sir. i didn't mean that!
    i have no one else to ask for a help in java. thatsy.

  14. #14
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    i changed the code like this:
    i know its not at all a great thing to just copy n paste the code u suggest.
    but am really thankful to you for helping me out by explaining all such small things ...

    Code:-

    Java Code:
    import java.io.*;
    
    public class phextract {
        public static void main(String[] args) {
            String[] lines = new String[0];
            String path = "input.txt";
            BufferedReader br = null;
            String phone;
            try {
                File file = new File(path);
                br = new BufferedReader(
                     new InputStreamReader(
                     new FileInputStream(file)));
                String text;
                while( (text = br.readLine()) != null ) { 
    
    String phoneNumberText = "PhoneNumber=";
    int findPhone = text.indexOf(phoneNumberText);
    if (findPhone != -1) {
    // Remove "PhoneNumber=" from the line, leaving you with
    // just the actual number
    phone = text.substring(findPhone + phoneNumberText.length() + 1);
    // Print to standard out
    System.out.println(phone);
    	}
       }
    	//Close everything!!!
    	br.close();
    	//in.close();
    }catch(IOException e) {System.err.println(e);} 
    catch(NullPointerException e) 
    { System.err.println("That file did not load"); }
    }
    }

  15. #15
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Sir eventhough i got the output correctly, i tried to change it by being adding some more strings like "Contact No:", "Ph No:", "Phone No:" along with "PhoneNumber=" by using String[].
    But now i got some compile errors and couldn't resolve it.
    i changed the code like this:

    Java Code:
    String phoneNumberText[] = new String [3];
    phoneNumberText[0]="Contact No:";
    phoneNumberText[1]="Ph No:";
    phoneNumberText[2]="Phone No:";
    int findPhone = text.indexOf(phoneNumberText[0]);
    int fp1=text.indexOf(phoneNumberText[1]);
    int fp2=text.indexOf(phoneNumberText[2]);
    if (findPhone != -1||fp1 != -1||fp2 != -1) {
    // Remove "PhoneNumber=" from the line, leaving you with just the actual number
    phone = text.substring(findPhone + phoneNumberText[].length() + 1);
    // Print to standard out
    System.out.println(phone);
    	}
       }
    The errors where on the line

    phone = text.substring(findPhone + phoneNumberText[].length() + 1);

    2 errors !!
    one was class expected and the other was ')' expected




  16. #16
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    You should always post what errors you have found. The actual output from the compiler...

    Hmmm, lol! Hello! If you took some time to look at your code you would see the empty [] right in front of you! You forgot to specify which String from the array of strings you wanted so Java gave you an error. I am not too sure which string you wanted so I will leave that up to you.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  17. #17
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Ohh..!! Now i got it...!! thank you thank you thankkkk youu for pointing out the mistake i did....

  18. #18
    durga.laxmi is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    i have another doubt now..
    my input.txt file contains the following details

    Toyota Ph No: 1456328795
    Subaru contact No: 6954013010
    Volkswagen Phone Number : 5432869823

    My code works well with this...
    but the problem is that if i gave any other details in the same line after the phone number, that will also get printed...
    i only need the phone number to be printed ...how can i make that possible..?? can anybody give me some clue..

    ie if my input.txt file now contains details like this:
    Toyota Ph No: 1456328795 email: abc@abcmail.com
    Subaru contact No: 6954013010
    Volkswagen Phone Number : 5432869823

    now my output will change to
    1456328795 email: abc@abcmail.com
    6954013010
    5432869823


    so i want to omit everything that comes after the phone number now...

  19. #19
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Options:
    1. Hire a Java programmer.
    2. See below.


    Java Code:
    					int firstCharOfPhoneNumber = findPhone + phoneNumberText.length() + 1;
    					phone = text.substring(firstCharOfPhoneNumber, firstCharOfPhoneNumber + 10);
    (Why am I such a nice guy?!? )

  20. #20
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Quote Originally Posted by durga.laxmi View Post
    i have another doubt now..

    Toyota Ph No: 1456328795 email: abc@abcmail.com
    Subaru contact No: 6954013010
    Volkswagen Phone Number : 5432869823

    so i want to omit everything that comes after the phone number now...
    Why are you making things hard on yourself. You should just give email it's own line, or follow the above, but still try to look for the easiest option even when doing something that needs a complete algorithm.
    JeffGrigg likes this.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Page 1 of 2 12 LastLast

Posting Permissions

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