Results 1 to 7 of 7
  1. #1
    pitmanr2003 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Begginer needs help with read in file assignment.

    I am having a really hard time with this assignment. I have tried everything I can think of for 2 days and the more I try the more I get confused.

    Here is what im trying to do. We are asked to read in a file that is in a format that looks like this

    smith#john#8632247568#25
    and so on.

    each number sign represents the stop of a string that i need to pull out and assign to a variable and output in this format:

    Name Phone Number SMS

    John Smith (863)224-7568 25

    and so on till the end of the file.

    I first tried to use substring method to do this and select # symbol, but everytime i do it just deletes first name before it when it prints. Im trying delimiter method now, but it outputs somewhat right but then outputs and error. here is sample of my code. It is in very ruff shape so excuss the mess.
    Java Code:
    //********************************************************************
    //  URLDissector.java       Java Foundations
    //
    //  Demonstrates the use of Scanner to read file input and parse it
    //  using alternative delimiters.
    //********************************************************************
    
    import java.util.Scanner;
    import java.io.*;
    
    public class phoneRecords
    {
       //-----------------------------------------------------------------
       //  Reads urls from a file and prints their path components.
       //-----------------------------------------------------------------
       public static void main (String[] args) throws IOException
       {
    String test;
    String last;
    String first;
          String name;
          String number;
          String sms;
          Scanner nameScan;
    
            System.out.println ("Name\tPhone\tSMS");
    	        System.out.println ();
    
    
          nameScan = new Scanner (new File("phoneRecords.txt"));
          nameScan.useDelimiter("#");
    
    
          while (nameScan.hasNext())
    
    	        {
    				last = nameScan.next();
    				System.out.println();
    				first = nameScan.next();
    				number = nameScan.next();
    				sms = nameScan.next();
    
    
    System.out.println(first + last + number + sms);
    
    }
    
    
      }
    }
    Last edited by Norm; 02-17-2012 at 01:00 PM. Reason: added code tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Begginer needs help with read in file assignment.

    I wouldn't use Scanner.
    Just read each line in using a normal BufferedReader, that way you know you have a single entry.
    Then split() the String using your delimiter.

    Scanner has some quirks that can be counterintuitive to some.
    For example, I'm not sure how the above code will handle newline characters (which I suspect is your problem).

  3. #3
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Begginer needs help with read in file assignment.

    On top ad:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    and then:

    Java Code:
    String line;
    
    FileReader vstream = new FileReader("phoneRecords.txt");
    BufferedReader in = new BufferedReader(vstream);
    line=in.readLine();
    Then the string called line contains "smith#john#8632247568#25" and then you can split the string.
    Last edited by F.S.; 02-17-2012 at 04:37 PM.

  4. #4
    pitmanr2003 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Begginer needs help with read in file assignment.

    thx for the suggestions.

    here is the problem with that though.
    teacher wants us to do it a certain way. also there are several more lines in the txt, that was just a sample of one line. the program takes a txt file of several peoples name, phone numbers, and how many messages they sent and each part is only seperated by a #

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Begginer needs help with read in file assignment.

    OK then, Scanner.nextLine() as the last line in the loop.
    If that is not a cure then you;re going to have to show us what is begin output with the current code.

  6. #6
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Begginer needs help with read in file assignment.

    I have no experience with Scanner, but this is how I solved it.
    Maybe you can adjust this program to do it with Scanner:

    Java Code:
    package readfile;
    
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    /**
     *
     * @author F.S.
     */
    public class Readfile {
      
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
          String line, symbol="#";
          String[] temp;
          String firstname, lastname, name, number, sms;
          try{
            FileReader vstream = new FileReader("phoneRecords.txt");
            BufferedReader in = new BufferedReader(vstream);
            while((line=in.readLine()) != null){
                temp = line.split(symbol);
                lastname = temp[0];
                firstname = temp[1];
                number = "("+temp[2].substring(0,3)+")"+temp[2].substring(3,6)+"-"+temp[2].substring(6);
                sms = temp[3];            
                name=firstname+" "+lastname;
                System.out.println(name+" "+number+" "+sms);
            }
            in.close();
          }
          catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
          }
        }
    }
    Last edited by F.S.; 02-17-2012 at 04:36 PM.

  7. #7
    pitmanr2003 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Begginer needs help with read in file assignment.

    ok im getting somewhere on it. I figured out how to get the first indexOf # and assign it to a variable so that it will print first name. Now i just need to repeat it for the next # in the file and so on. I cant for the life of me figure out how to get the second indexOf # and so on.

Similar Threads

  1. begginer needs help..?
    By lina in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 04:48 PM
  2. Begginer and project
    By Gym in forum New To Java
    Replies: 6
    Last Post: 03-24-2009, 01:30 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Student--begginer--help!!!
    By AmandaIT in forum New To Java
    Replies: 6
    Last Post: 05-09-2008, 03:37 AM
  5. Tic Tac Toe Ggame Help (Begginer)
    By zero2008 in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 10:18 PM

Posting Permissions

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