Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Manipulating Strings in a File Scanner

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Manipulating Strings in a File Scanner

    I was given a text file that has list of names phone numbers, calls in and out ect... Like this

    Adams#Marilyn#8233331109#0#0#01012014#C
    Anderson#John#5025559980#20#15#12152013#M
    Baker-Brown#Angelica#9021329944#0#3#02112014#C

    The # are delimiters between data items and each line has the call status as the last item. I need to know how I can display each persons information on the screen in a format such as:

    Name Phone Calls Out Calls In Last Call

    Marilyn Adams (823) 333-1109 0 0 01-01-2104
    John Anderson (502) 555-9980 20 15 12-15-2013
    Angelica Baker-Brown (859) 254-1109 11 5 02-11-2014

    I have to use substring method to extract the phone number and add parentheses/dashes ect I also must have a while statement and a delimiter...

    So Far my code looks like this Also I am in a beginners Java coding class....

    import java.util.Scanner;
    import java.io.*;
     
    public class phonedata2_1
    {
      public static void main (String[] args) throws IOException
      {
     
       String Phonefile, FirstName, LastName;
       Scanner PhoneScan, fileScan;
     
       System.out.println ("    Name      Phone      Calls Out       Calls In       Last Call           Status");
     
       fileScan = new Scanner(new File("phonedata.txt"));
     
       while (fileScan.hasNext())
       {
    Phonefile = fileScan.nextLine();
     
        PhoneScan = new Scanner(Phonefile);
        PhoneScan.useDelimiter("#");
     
    System.out.println(PhoneScan.next()+" "+
    PhoneScan.next()+"\t" +
    PhoneScan.next()+"\t" +
    PhoneScan.next()+"\t" +
    PhoneScan.next()+"\t" +
    PhoneScan.next()+"\t" +
    PhoneScan.next());
    }
       System.out.println ("\nTotal outgoing calls for the period: " + "\nTotal incoming calls    for the period: \n");
     
       }
    }
    Attached Files Attached Files


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,375
    My Mood
    Mellow
    Thanks
    254
    Thanked 787 Times in 772 Posts

    Default Re: Manipulating Strings in a File Scanner

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    What's your question? What do you need help with?

    Remember that class names are capitalized while variable and method names begin with lowercase letters. Not following this convention makes your code more difficult to read and understand.

    Have you reviewed the substring() method in the String API? You might also look at the charAt() method while you're there.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Manipulating Strings in a File Scanner

    I just need to know how I can take information from a File, which I have attached In my above post, then while using a delimiter grab the phone numbers and substring them to become (###)###-#### in that format.

    I have the File in my Java editor and I even have the delimiter working. But I can not figure out how to use a substring to edit the phone numbers.

    Adams#Marilyn#8233331109#0#0#01012014#C
    Anderson#John#5025559980#20#15#12152013#M
    Baker-Brown#Angelica#9021329944#0#3#02112014#C

    This needs to look like

    Marilyn Adams (823) 333-1109 0 0 01-01-2104
    John Anderson (502) 555-9980 20 15 12-15-2013
    Angelica Baker-Brown (859) 254-1109 11 5 02-11-2014

    Which I am half way there its the substring I keep getting caught up on since its pretty much in a file and I dont understand how I can make that phone number a string to do that with.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,375
    My Mood
    Mellow
    Thanks
    254
    Thanked 787 Times in 772 Posts

    Default Re: Manipulating Strings in a File Scanner

    In the first post you said you had to use the substring() method, now I'm not so sure that you have to use substring() and that you're really asking for the best approach to accomplish what you need to do using a delimiter. If you can, I recommend you use the String.split() method.

    Clarify your requirements. In future, only include solutions that you must use to give us the freedom to guide you to other solutions that might be better than what you've chosen.

Similar Threads

  1. PrintStream, strings and reading from a file.
    By taze in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 1st, 2013, 09:50 PM
  2. Help displaying strings that appear in one text file but not another!
    By deepcrimson76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 09:08 AM
  3. Read Text File Into Strings
    By Khadafi in forum Java Theory & Questions
    Replies: 31
    Last Post: January 14th, 2012, 09:29 AM
  4. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  5. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 10:13 AM

Tags for this Thread